{"id":3826,"date":"2018-07-29T19:07:03","date_gmt":"2018-07-29T10:07:03","guid":{"rendered":"https:\/\/www.ws-meguro.com\/\/?p=3826"},"modified":"2018-07-30T18:48:15","modified_gmt":"2018-07-30T09:48:15","slug":"javascript%e3%81%ae%e7%b7%b4%e7%bf%92%ef%bc%9a%e7%b0%a1%e5%8d%98%e3%81%aa%e3%82%a2%e3%83%8b%e3%83%a1%ef%bc%88%e3%83%9c%e3%83%bc%e3%83%ab%e3%81%8c%e5%8b%95%e3%81%8f%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.wsmeguro.jp\/wp\/?p=3826","title":{"rendered":"JavaScript\u306e\u7df4\u7fd2\uff1a\u7c21\u5358\u306a\u30a2\u30cb\u30e1\uff08\u30dc\u30fc\u30eb\u304c\u52d5\u304f\uff09"},"content":{"rendered":"<p>\u696d\u52d9\u7cfb\u306e\u30a2\u30d7\u30ea\u3064\u304f\u308a\u304c\u76ee\u7684\u306a\u3093\u3067\u3059\u304c\u3001\u3084\u306f\u308a\u3053\u3046\u3044\u3046\u306e\u306f\u907f\u3051\u3066\u901a\u308c\u306a\u3044\u307f\u305f\u3044\u3067\u3059\u3002<a href=\"https:\/\/www.wsmeguro.jp\/\/tools\/js\/test6\/\" rel=\"noopener\" target=\"_blank\">\u3067\u304d\u305f\u3082\u306e\u306f\u3053\u3061\u3089<\/a>\u3002<br \/>\n\u30dc\u30fc\u30eb\u304c\u3042\u3063\u3061\u3053\u3063\u3061\u306b\u52d5\u304d\u307e\u3059\u3002canvas\u306e\u7aef\u306b\u6765\u308b\u3068\u901f\u5ea6\u306e\u7b26\u53f7\u3092\u5909\u3048\u3066\u5b8c\u5168\u5f3e\u6027\u885d\u7a81\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u3053\u308d\u304c\u300c\u307b\u30fc\u304a\u300d\u3068\u3046\u306a\u3065\u3051\u308b\u3068\u3053\u308d\u3067\u3057\u305f\u3002<br \/>\n\u30a2\u30cb\u30e1\u306b\u3059\u308b\u306b\u306f\u3001\u30a2\u30cb\u30e1\u306e\u30eb\u30fc\u30d7\u95a2\u6570\u306e\u4e2d\u306b\u300crequestAnimationFrame(loop);\u300d\u3092\u5165\u308c\u3066\u3001loop()\u3092\u4e00\u5ea6\u3001\u5916\u304b\u3089\u547c\u3073\u51fa\u3059\u5fc5\u8981\u304c\u3042\u308b\u3088\u3046\u3067\u3059\u3002<br \/>\n\u3042\u3068\u3001\u8ecc\u8de1\u304c\u6b8b\u308b\u306e\u306f\u3001\u300cctx.fillStyle = &#8216;rgba(0, 0, 0, 0.25)&#8217;\u300d\u3067\u30010.25\u306e\u534a\u900f\u660e\u3092\u6307\u5b9a\u3057\u3066\u304a\u308a\u3001\u904e\u53bb4\u56de\u5206\u304c\u5c11\u3057\u305a\u3064\u8272\u3092\u8584\u3081\u3064\u3064\u6b8b\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002\u3046\u307e\u304f\u3067\u304d\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >Ball.prototype.update = function() {\r\n  if ((this.x + this.size) &gt;= width) {\r\n    this.velX = -(this.velX);\r\n  }\r\n  if ((this.x - this.size) &lt;= 0) {\r\n    this.velX = -(this.velX);\r\n  }\r\n  if ((this.y + this.size) &gt;= height) {\r\n    this.velY = -(this.velY);\r\n  }\r\n  if ((this.y - this.size) &lt;= 0) {\r\n    this.velY = -(this.velY);\r\n  }\r\n  this.x += this.velX;\r\n  this.y += this.velY;\r\n}<\/pre>\n<p><!--more--><br \/>\nHTML\u306f\u3053\u3061\u3089<\/p>\n<pre class=\"lang:python decode:true \" >\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;meta charset=\"utf-8\"&gt;\r\n    &lt;title&gt;Bouncing balls&lt;\/title&gt;\r\n    &lt;link rel=\"stylesheet\" href=\"style.css\"&gt;\r\n  &lt;\/head&gt;\r\n\r\n  &lt;body&gt;\r\n    &lt;h1&gt;bouncing balls&lt;\/h1&gt;\r\n    &lt;canvas&gt;&lt;\/canvas&gt;\r\n\r\n    &lt;script src=\"script.js\"&gt;&lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>JavaScript\u306f\u3053\u3061\u3089\u3002<\/p>\n<pre class=\"lang:js decode:true \" >\/\/ setup canvas\r\n\r\nvar canvas = document.querySelector('canvas');\r\nvar ctx = canvas.getContext('2d');\r\n\r\nvar width = canvas.width = window.innerWidth;\r\nvar height = canvas.height = window.innerHeight;\r\n\r\nvar testBall = new Ball(50, 100, 4, 4, 'blue', 10);\r\n\r\n\/\/ function to generate random number\r\n\r\nfunction random(min,max) {\r\n  var num = Math.floor(Math.random()*(max-min)) + min;\r\n  return num;\r\n}\r\n\r\nfunction Ball(x, y, velX, velY, color, size) {\r\n    this.x = x;\r\n    this.y = y;\r\n    this.velX = velX;\r\n    this.velY = velY;\r\n    this.color = color;\r\n    this.size = size;\r\n  }\r\n\r\nBall.prototype.draw = function() {\r\n    ctx.beginPath();\r\n    ctx.fillStyle = this.color;\r\n    ctx.arc(this.x, this.y, this.size, 0, 2 * Math.PI);\r\n    ctx.fill();\r\n}\r\n\r\nBall.prototype.update = function() {\r\n    if ((this.x + this.size) &gt;= width) {\r\n      this.velX = -(this.velX);\r\n    }\r\n  \r\n    if ((this.x - this.size) &lt;= 0) {\r\n      this.velX = -(this.velX);\r\n    }\r\n  \r\n    if ((this.y + this.size) &gt;= height) {\r\n      this.velY = -(this.velY);\r\n    }\r\n  \r\n    if ((this.y - this.size) &lt;= 0) {\r\n      this.velY = -(this.velY);\r\n    }\r\n  \r\n    this.x += this.velX;\r\n    this.y += this.velY;\r\n  }\r\n\r\n  var balls = [];\r\n  function loop() {\r\n    ctx.fillStyle = 'rgba(0, 0, 0, 0.25)';\r\n    ctx.fillRect(0, 0, width, height);\r\n  \r\n    while (balls.length &lt; 25) {\r\n      var size = random(10,20);\r\n      var ball = new Ball(\r\n        \/\/ ball position always drawn at least one ball width\r\n        \/\/ away from the adge of the canvas, to avoid drawing errors\r\n        random(0 + size,width - size),\r\n        random(0 + size,height - size),\r\n        random(-7,7),\r\n        random(-7,7),\r\n        'rgb(' + random(0,255) + ',' + random(0,255) + ',' + random(0,255) +')',\r\n        size\r\n      );\r\n      balls.push(ball);\r\n    }\r\n  \r\n    for (var i = 0; i &lt; balls.length; i++) {\r\n      balls[i].draw();\r\n      balls[i].update();\r\n      balls[i].collisionDetect();\r\n    }\r\n  \r\n    requestAnimationFrame(loop);\r\n  }\r\n\r\n  Ball.prototype.collisionDetect = function() {\r\n    for (var j = 0; j &lt; balls.length; j++) {\r\n      if (!(this === balls[j])) {\r\n        var dx = this.x - balls[j].x;\r\n        var dy = this.y - balls[j].y;\r\n        var distance = Math.sqrt(dx * dx + dy * dy);\r\n  \r\n        if (distance &lt; this.size + balls[j].size) {\r\n          balls[j].color = this.color = 'rgb(' + random(0, 255) + ',' + random(0, 255) + ',' + random(0, 255) +')';\r\n        }\r\n      }\r\n    }\r\n  }\r\n\r\n    loop();<\/pre>\n<p>CSS\u306f\u3053\u3061\u3089<\/p>\n<pre class=\"lang:python decode:true \" >\r\nhtml, body {\r\n    margin: 0;\r\n  }\r\n  \r\n  html {\r\n    font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;\r\n    height: 100%;\r\n  }\r\n  \r\n  body {\r\n    overflow: hidden;\r\n    height: inherit;\r\n  }\r\n  \r\n  h1 {\r\n    font-size: 2rem;\r\n    letter-spacing: -1px;\r\n    position: absolute;\r\n    margin: 0;\r\n    top: -4px;\r\n    right: 5px;\r\n  \r\n    color: transparent;\r\n    text-shadow: 0 0 4px white;\r\n  }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u696d\u52d9\u7cfb\u306e\u30a2\u30d7\u30ea\u3064\u304f\u308a\u304c\u76ee\u7684\u306a\u3093\u3067\u3059\u304c\u3001\u3084\u306f\u308a\u3053\u3046\u3044\u3046\u306e\u306f\u907f\u3051\u3066\u901a\u308c\u306a\u3044\u307f\u305f\u3044\u3067\u3059\u3002\u3067\u304d\u305f\u3082\u306e\u306f\u3053\u3061\u3089\u3002 \u30dc\u30fc\u30eb\u304c\u3042\u3063\u3061\u3053\u3063\u3061\u306b\u52d5\u304d\u307e\u3059\u3002canvas\u306e\u7aef\u306b\u6765\u308b\u3068\u901f\u5ea6\u306e\u7b26\u53f7\u3092\u5909\u3048\u3066\u5b8c\u5168\u5f3e\u6027\u885d\u7a81\u306e\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u3053\u308d\u304c\u300c\u307b&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[560],"tags":[558,586,587,588],"class_list":["post-3826","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript","tag-requestanimationframe","tag-587","tag-588"],"_links":{"self":[{"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3826"}],"version-history":[{"count":4,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3826\/revisions"}],"predecessor-version":[{"id":3830,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3826\/revisions\/3830"}],"wp:attachment":[{"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsmeguro.jp\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}