JavaScript WebGL 

ICS
2018 7 25 @ Frontend de KANPAI! #4 - -
(Yasunobu Ikeda)
ICS /
@clockmaker
HSL
: ICS MEDIA
ICS MEDIA - Web /
https://ics.media/
2D 

https://github.com/ics-creative/180725_two_waves
3D 

https://github.com/ics-creative/180725_three_waves
URL
JS
Canvas JavaScript
CreateJS 2D gskinner
Context2D
Adobe Animate CC
PixiJS 2D GoodBoyDigital
Flash
WebGL
WebGL
Three.js 3D mr.doob WebGL
BabylonJS 3D Microsoft 3D Unity
ICS
CreateJS 2012 2016
PixiJS
WebGL 

CreateJS PixiJS
2017
Three.js 3D 2015
Babylon.js Three.js Babylon.js 2018
ICS Babylon.js
CEDEC 2018
https://2018.cedec.cesa.or.jp/session/detail/s5abfa25af2045
8月22日(水)
16:30
2D 3D
context.moveTo(0, stageH / 2);
context.lineTo(stageW, stageH / 2);
context.stroke();
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/line.html
[...Array(分割数).keys()].forEach(i => {
const x = i / (分割数 - 1) * stageW;
const ラジアン = i / 分割数 * Math.PI + 時間;
const y = 振幅 * Math.sin(ラジアン) + stageH / 2;
context.lineTo(x, y);
context.stroke();
});
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/line_sin.htmlKeynote
const y = 振幅 * Math.sin(ラジアン);
const 分割数 = 100; // 分割数
[...Array(分割数).keys()].forEach(i => {
//・・・
context.lineTo(x, y);
});
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/line_sin.htmlKeynote
npmJS JS
simplenoise - npm
https://www.npmjs.com/package/simplenoise
x
y
0.1
x+0.1
y
0.15
x+0.2
y
0.2
const value = noise.perlin2(x, y)
[...Array(分割数).keys()].forEach(i => {
const x = i / (分割数 - 1) * stageW;
const px = i / 50; // 横軸の⼊⼒値
const py = 時間; // 時間の⼊⼒値
const y = amplitude * noise.perlin2(px, py) + stageH / 2;
context.lineTo(x, y);
});
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/line_perline.htmlKeynote
[...Array(線数).keys()].forEach(j => {
[...Array(分割数).keys()].forEach(i => {
const x = i / (分割数 - 1) * stageW;
const px = i / (50 + j);
const py = j / 50 + time;
const y = 振幅 * noise.perlin2(px, py) + stageH / 2;
context.lineTo(x, y);
});
context.stroke();
});
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/line_perlines.htmlKeynote
HSL
CreateJS HSL - ICS MEDIA
https://ics.media/tutorial-createjs/color_hsl.html
const MAX = 20;
[...Array(MAX).keys()].forEach(i => {
// HSLカラーを算出
const hue = i * (360 / MAX);
const color = `hsl(${hue}, 100%, 50%)`;
// (省略)
})
[...Array(線数).keys()].forEach(j => {
const h = Math.round(j / lineNum * 360); // ⾊相
const s = 100; // 彩度
const l = Math.round(j / lineNum * 100); // 明度
context.strokeStyle = `hsl(${h}, ${s}%, ${l}%)`;
[...Array(分割数).keys()].forEach(i => {
// ・・・
});
context.stroke();
});
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/line_perlines_colorful.htmlKeynote
=
CG
particle.vy -= 1; // 重⼒
particle.vy *= 0.92; // 摩擦
particle.y += particle.vy; // 速度を座標へ
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/particle_0.htmlKeynote
// パーティクルのサイズを寿命に⽐例にする
const scale = particle.life / MAX_LIFE;
particle.scale = scale;
particle.life -= 1; // 寿命を減らす
if (particle.life <= 0) { // 寿命の判定
// 削除
}
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/particle_1.htmlKeynote
function tick() {
requestAnimationFrame(tick);
// パーティクルを発⽣
emitParticles();
// ・・・
}
// パーティクルを発⽣させます
function emitParticles() {
// パーティクルの⽣成
for (let i = 0; i < 1; i++) {
const particle = new Particle();
particles.push(particle);
}
}
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/particle_3.htmlKeynote
context.beginPath();
context.arc(particle.x, particle.y, ・・・);
const alpha = Math.random() * 0.2 + 0.8;
switch (particle.type) {
case '0':
context.fillStyle = `hsla(0, 0%, 50%, ${alpha})`;
context.fill();
break;
case '1':
context.strokeStyle = `hsla(0, 0%, 50%, ${alpha})`;
context.lineWidth = 5;
context.stroke();
break;
}
context.closePath();
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/particle_3.htmlKeynote
CreateJS - ICS MEDIA
https://ics.media/tutorial-createjs/particle.html
(GC)
GC
particle = new Particle() particle.dispose()
particle = null
GC
particle = fromPool()
particle.reset()
toPool(particle)
GC
const objectPool = []; // オブジェクトプール
function toPool(particle) {
// 使⽤済み粒⼦はプールに移動
objectPool.unshift(particle);
}
function fromPool() {
if (objectPool.length === 0) {
// プールが空なら新規⽣成
return new Particle();
} else {
// プールにストックがあれば取り出す
return objectPool.pop();
}
}
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/particle_4.htmlKeynote
Object Pool FAL Works
https://www.fal-works.com/creative-coding-posts/object-pool
- Qiita
https://qiita.com/bkzen/items/d84312246061778ee870
Math.random()


:
const x = Math.random();
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/particle_random.htmlKeynote
:
const x = (Math.random() + Math.random()) / 2;
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/particle_random.htmlKeynote
:
const valueA = (Math.random() + Math.random()) / 2;
const valueB = (Math.random() + Math.random()) / 2;
const x = (valueA + valueB) / 2;
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/particle_random.htmlKeynote
:
const base = Math.random() * Math.random() * Math.random();
const inverse = 1.0 - base;
const x = Math.random() < 0.5 ? base : inverse;
▶ Play sample with new window
https://ics-creative.github.io/180725_two_waves/particle_random.htmlKeynote
JavaScript - ICS MEDIA
https://ics.media/entry/11292
Three.js 3
Three.js 3
HSL
Adobe Sensei Photoshop XD - ICS MEDIA
https://ics.media/entry/16558
2011 Flash 2015 CreateJS 2018 Three.js2009 Flash
Flash WebGL
Twitter
ICS
ikeda@ics-web.jp
@clockmaker
Copyright 2018 ICS INC. All rights reserved.

JavaScriptとWebGLで取り組む
クリエイティブコーディング