LoginSignup
8
6

More than 5 years have passed since last update.

【CodePen】JavaScriptとcanvasで誘導弾パーティクル

Posted at

CodePenが埋め込めるようになった記念に、誘導弾風のアルゴリズムでパーティクルがマウスに集まってくるという先日作ったサンプルを埋めてみます。

See the Pen Homing Particles by Akira Ikeda (@akicho8) on CodePen.

技術的におもしろいところは描画領域の消去方法です。他の方のコードを読みまくって知ったのですが、clearRect で全消去するのではなく次のように描画方法を destination-out かつ半透明で描画すると徐々に消えていく効果が得られることがわかりました。

ctx.globalCompositeOperation = "destination-out"
ctx.fillStyle = "rgba(0, 0, 0, 0.2)"
ctx.fillRect(0, 0, w, h)

またパーティクルのオブジェクトの方は lighter で描画することで重なった部分が光っているような効果になります。

ctx.globalCompositeOperation = "lighter"

参照

globalCompositeOperation プロパティ - Canvasリファレンス - HTML5.JP
http://www.html5.jp/canvas/ref/property/globalCompositeOperation.html

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6