{"id":4469,"date":"2023-03-28T22:43:32","date_gmt":"2023-03-28T13:43:32","guid":{"rendered":"https:\/\/engineering.webstudio168.jp\/?p=4469"},"modified":"2023-03-29T01:47:57","modified_gmt":"2023-03-28T16:47:57","slug":"bubble-javascript-gimmick","status":"publish","type":"post","link":"https:\/\/engineering.webstudio168.jp\/blog\/bubble-javascript-gimmick\/","title":{"rendered":"\u6ce1\u304c\u4e0b\u304b\u3089\u51fa\u308bJavaScript\u30ae\u30df\u30c3\u30af\u3092Canvas\u3067\u63cf\u753b\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u3010jQuery\u7121\u3057\u3011"},"content":{"rendered":"\n<canvas id=\"myCanvas\"><\/canvas>\n\n<script>\n  \/\/ Canvas\u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b\n  const canvas = document.getElementById('myCanvas');\n\n  \/\/ Canvas\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u53d6\u5f97\u3059\u308b\n  const ctx = canvas.getContext('2d');\n\n  \/\/ \u6ce1\u3092\u8868\u3059Bubble\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3059\u308b\n  class Bubble {\n    constructor(x, y, radius, color) {\n      this.x = x;\n      this.y = y;\n      this.radius = radius;\n      this.color = color;\n      this.dy = Math.random() * 2 + 1;\n    }\n\n    \/\/ \u6ce1\u3092\u63cf\u753b\u3059\u308b\u30e1\u30bd\u30c3\u30c9\n    draw() {\n      ctx.beginPath();\n      ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);\n      ctx.fillStyle = this.color;\n      ctx.fill();\n      ctx.closePath();\n    }\n\n    \/\/ \u6ce1\u3092\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3055\u305b\u308b\u30e1\u30bd\u30c3\u30c9\n    update() {\n      \/\/ \u6ce1\u306ey\u5ea7\u6a19\u3092\u66f4\u65b0\u3059\u308b\n      this.y -= this.dy;\n\n      \/\/ \u6ce1\u304c\u753b\u9762\u5916\u306b\u51fa\u305f\u3089\u65b0\u3057\u3044\u6ce1\u3068\u3057\u3066\u518d\u751f\u6210\u3059\u308b\n      if (this.y + this.radius < 0) {\n        this.x = Math.random() * canvas.width;\n        this.y = canvas.height + this.radius;\n        this.dy = Math.random() * 2 + 1;\n      }\n\n      \/\/ \u6ce1\u3092\u63cf\u753b\u3059\u308b\n      this.draw();\n    }\n  }\n\n  \/\/ \u6ce1\u306e\u914d\u5217\u3092\u4f5c\u6210\u3059\u308b\n  const bubbles = [];\n  for (let i = 0; i < 20; i++) {\n    const x = Math.random() * canvas.width;\n    const y = canvas.height + Math.random() * 200;\n    const radius = Math.random() * 20 + 10;\n    const color = `rgba(255, 255, 255, ${Math.random() * 0.5 + 0.5})`;\n    bubbles.push(new Bubble(x, y, radius, color));\n  }\n\n  \/\/ \u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3059\u308b\n  function animate() {\n    requestAnimationFrame(animate);\n\n    \/\/ Canvas\u3092\u30af\u30ea\u30a2\u3059\u308b\n    ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n    \/\/ \u6ce1\u3092\u66f4\u65b0\u3059\u308b\n    for (let i = 0; i < bubbles.length; i++) {\n      bubbles[i].update();\n    }\n  }\n  animate();\n<\/script>\n<style>\n  canvas#myCanvas {\n    background-color: #000;\n    width: 100%;\n  }\n<\/style>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce1\uff08\u5186\uff09\u304c\u4e0b\u304b\u3089\u30e9\u30f3\u30c0\u30e0\u306b\u767b\u5834\u3059\u308b\u30ae\u30df\u30c3\u30af\u3092canvas\u4e0a\u306b\u63cf\u753b\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u4f55\u304b\u306e\u5fc5\u8981\u304c\u3042\u308c\u3070\u3054\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-html\" data-file=\"canvas-bubble-js.html\" data-lang=\"HTML\"><code>&lt;canvas id=&quot;myCanvas&quot;&gt;&lt;\/canvas&gt;\n\n&lt;script&gt;\n  \/\/ Canvas\u8981\u7d20\u3092\u53d6\u5f97\u3059\u308b\n  const canvas = document.getElementById(&#39;myCanvas&#39;);\n\n  \/\/ Canvas\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u53d6\u5f97\u3059\u308b\n  const ctx = canvas.getContext(&#39;2d&#39;);\n\n  \/\/ \u6ce1\u3092\u8868\u3059Bubble\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3059\u308b\n  class Bubble {\n    constructor(x, y, radius, color) {\n      this.x = x;\n      this.y = y;\n      this.radius = radius;\n      this.color = color;\n      this.dy = Math.random() * 2 + 1;\n    }\n\n    \/\/ \u6ce1\u3092\u63cf\u753b\u3059\u308b\u30e1\u30bd\u30c3\u30c9\n    draw() {\n      ctx.beginPath();\n      ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);\n      ctx.fillStyle = this.color;\n      ctx.fill();\n      ctx.closePath();\n    }\n\n    \/\/ \u6ce1\u3092\u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3055\u305b\u308b\u30e1\u30bd\u30c3\u30c9\n    update() {\n      \/\/ \u6ce1\u306ey\u5ea7\u6a19\u3092\u66f4\u65b0\u3059\u308b\n      this.y -= this.dy;\n\n      \/\/ \u6ce1\u304c\u753b\u9762\u5916\u306b\u51fa\u305f\u3089\u65b0\u3057\u3044\u6ce1\u3068\u3057\u3066\u518d\u751f\u6210\u3059\u308b\n      if (this.y + this.radius &lt; 0) {\n        this.x = Math.random() * canvas.width;\n        this.y = canvas.height + this.radius;\n        this.dy = Math.random() * 2 + 1;\n      }\n\n      \/\/ \u6ce1\u3092\u63cf\u753b\u3059\u308b\n      this.draw();\n    }\n  }\n\n  \/\/ \u6ce1\u306e\u914d\u5217\u3092\u4f5c\u6210\u3059\u308b\n  const bubbles = [];\n  for (let i = 0; i &lt; 20; i++) {\n    const x = Math.random() * canvas.width;\n    const y = canvas.height + Math.random() * 200;\n    const radius = Math.random() * 20 + 10;\n    const color = `rgba(255, 255, 255, ${Math.random() * 0.5 + 0.5})`;\n    bubbles.push(new Bubble(x, y, radius, color));\n  }\n\n  \/\/ \u30a2\u30cb\u30e1\u30fc\u30b7\u30e7\u30f3\u3092\u5b9f\u884c\u3059\u308b\n  function animate() {\n    requestAnimationFrame(animate);\n\n    \/\/ Canvas\u3092\u30af\u30ea\u30a2\u3059\u308b\n    ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n    \/\/ \u6ce1\u3092\u66f4\u65b0\u3059\u308b\n    for (let i = 0; i &lt; bubbles.length; i++) {\n      bubbles[i].update();\n    }\n  }\n  animate();\n&lt;\/script&gt;\n&lt;style&gt;\n  canvas#myCanvas {\n    background-color: #000;\n    width: 100%;\n  }\n&lt;\/style&gt;<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u3046\u3053\u3068\u3042\u308b\u304b\u5fae\u5999\u3067\u3059\u304ccanvas\u306e\u63cf\u753b\u51e6\u7406\u306e\u3054\u53c2\u8003\u306b\u306a\u308c\u3070\u5149\u6804\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6ce1\uff08\u5186\uff09\u304c\u4e0b\u304b\u3089\u30e9\u30f3\u30c0\u30e0\u306b\u767b\u5834\u3059\u308b\u30ae\u30df\u30c3\u30af\u3092canvas\u4e0a\u306b\u63cf\u753b\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u4f55\u304b\u306e\u5fc5\u8981\u304c\u3042\u308c\u3070\u3054\u53c2\u8003\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002 \u4f7f\u3046\u3053\u3068\u3042\u308b\u304b\u5fae\u5999\u3067\u3059\u304ccanvas\u306e\u63cf\u753b\u51e6\u7406\u306e\u3054\u53c2\u8003\u306b\u306a\u308c\u3070\u5149\u6804\u3067\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":4474,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[8,91],"class_list":["post-4469","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-javascript","tag-91"],"_links":{"self":[{"href":"https:\/\/engineering.webstudio168.jp\/blog\/wp-json\/wp\/v2\/posts\/4469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/engineering.webstudio168.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/engineering.webstudio168.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/engineering.webstudio168.jp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/engineering.webstudio168.jp\/blog\/wp-json\/wp\/v2\/comments?post=4469"}],"version-history":[{"count":7,"href":"https:\/\/engineering.webstudio168.jp\/blog\/wp-json\/wp\/v2\/posts\/4469\/revisions"}],"predecessor-version":[{"id":4495,"href":"https:\/\/engineering.webstudio168.jp\/blog\/wp-json\/wp\/v2\/posts\/4469\/revisions\/4495"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/engineering.webstudio168.jp\/blog\/wp-json\/wp\/v2\/media\/4474"}],"wp:attachment":[{"href":"https:\/\/engineering.webstudio168.jp\/blog\/wp-json\/wp\/v2\/media?parent=4469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/engineering.webstudio168.jp\/blog\/wp-json\/wp\/v2\/categories?post=4469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/engineering.webstudio168.jp\/blog\/wp-json\/wp\/v2\/tags?post=4469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}