退屈なヒーローページもスパイスで味付け
カーソルで画像をパーティクルに変えるCSSとJavaScriptのテクニックです。使い方のアイデア次第では小さな驚きが生まれ、印象度を高めることができます。画像、文字、図形などを使ったモーショングラフィックスが可能です。スマホ・タブレット(指でのタッチ操作)への対応はしていません。対応することは可能です。
パーティクルアニメーション
サンプルコード
HTML
<div id="wpParticleWrapper" style="width:100%;max-width:1000px;margin:0 auto;">
<div id="particleContainer" style="width:100%;height:600px;background:#fff;overflow:hidden;position:relative;">
<div id="pErr" style="display:none;color:red;padding:10px;position:absolute;z-index:10;background:#fff;"></div>
<canvas id="pCanvas" style="display:block;width:100%;height:100%;"></canvas>
</div>
</div>
<script>
// ▼【画像URLと背景色の設定】▼
window.pConfigs = [
{ src: 'https://ml4dkiao0fgt.i.optimole.com/cb:JbRd.856/w:auto/h:auto/q:mauto/ig:avif/https://mon.co.jp/wp-content/uploads/2026/12/apple2.png', maxW: 0.90, maxH: 510, bg: 'rgba(0,0,0,0.05)' },
{ src: 'https://ml4dkiao0fgt.i.optimole.com/cb:JbRd.856/w:auto/h:auto/q:mauto/ig:avif/https://mon.co.jp/wp-content/uploads/2026/12/hyo-3.png', maxW: 0.95, maxH: 650, bg: 'rgba(0,0,0,0.05)' }
];
</script>
<script>
(function() {
function startParticleApp() {
const box = document.getElementById('particleContainer');
const cvs = document.getElementById('pCanvas');
const err = document.getElementById('pErr');
if (!box || !cvs) return;
if (!window.pConfigs) {
setTimeout(startParticleApp, 100);
return;
}
const ctx = cvs.getContext('2d', { willReadFrequently: true });
const dpr = Math.min(window.devicePixelRatio || 1, 2);
let W = box.clientWidth || 1000, H = 600, areas = [], pts = [];
const mouse = { x: null, y: null, r: 50 };
box.onmousemove = function(e) { const rc = cvs.getBoundingClientRect(); mouse.x = e.clientX - rc.left; mouse.y = e.clientY - rc.top; };
box.onmouseleave = function() { mouse.x = null; mouse.y = null; };
function build(img, dw, dh, sx, sy, bnd, bg) {
const oc = document.createElement('canvas');
oc.width = Math.round(dw * dpr); oc.height = Math.round(dh * dpr);
const octx = oc.getContext('2d');
octx.drawImage(img, 0, 0, oc.width, oc.height);
const id = octx.getImageData(0, 0, oc.width, oc.height), d = id.data;
if (d[3] >= 128) {
const r0 = d[0], g0 = d[1], b0 = d[2];
for (let i = 0; i < d.length; i += 4) {
if (Math.abs(d[i]-r0) + Math.abs(d[i+1]-g0) + Math.abs(d[i+2]-b0) <= 35) d[i+3] = 0;
}
octx.putImageData(id, 0, 0);
}
const pc = document.createElement('canvas');
pc.width = dw; pc.height = dh;
const pctx = pc.getContext('2d');
pctx.drawImage(oc, 0, 0, dw, dh);
const pd = pctx.getImageData(0, 0, dw, dh).data;
for (let y = 0; y < dh; y += 2) {
for (let x = 0; x < dw; x += 2) {
const idx = (y * dw + x) * 4;
if (pd[idx+3] <= 15) continue;
pts.push({
x: sx+x, y: sy+y, bx: sx+x, by: sy+y, vx: 0, vy: 0,
c: 'rgba(' + pd[idx] + ',' + pd[idx+1] + ',' + pd[idx+2] + ',' + (pd[idx+3]/255).toFixed(2) + ')',
sp: Math.random()*25 + 15, bnd: bnd, bg: bg, mv: false
});
}
}
return oc;
}
const items = window.pConfigs.map(function(cfg) {
const im = new Image();
const it = { im: im, cfg: cfg, ok: false };
fetch(cfg.src, { mode: 'cors', credentials: 'same-origin' })
.then(function(res) {
if (!res.ok) throw new Error('HTTP ' + res.status);
return res.blob();
})
.then(function(blob) {
const blobUrl = URL.createObjectURL(blob);
im.onload = function() { it.ok = true; setup(); };
im.src = blobUrl;
})
.catch(function() {
im.onload = function() { it.ok = true; setup(); };
im.onerror = function() { err.style.display = 'block'; err.innerHTML += '読込失敗: ' + cfg.src + '<br>'; };
im.src = cfg.src;
});
return it;
});
function setup() {
W = box.clientWidth || 1000;
const rt = W / 1000, pad = Math.round(45 * rt), gap = Math.round(100 * rt);
areas = []; pts = []; let curY = 0;
items.filter(function(i) { return i.ok && i.im.naturalWidth > 0; }).forEach(function(it, idx) {
if (idx > 0) curY += gap;
let sc = (W * it.cfg.maxW) / it.im.width;
if (it.im.height * sc > it.cfg.maxH * rt) sc = (it.cfg.maxH * rt) / it.im.height;
const dw = Math.floor(it.im.width * sc), dh = Math.floor(it.im.height * sc), ah = dh + pad * 2;
const ix = Math.floor((W - dw) / 2), iy = curY + pad;
const bnd = { x0: 0, x1: W, y0: curY, y1: curY + ah };
const cc = build(it.im, dw, dh, ix, iy, bnd, it.cfg.bg);
areas.push({ cc: cc, x: 0, y: curY, w: W, h: ah, bg: it.cfg.bg, ix: ix, iy: iy, iw: dw, ih: dh });
curY += ah;
});
H = Math.max(curY, Math.round(600 * rt));
box.style.height = H + 'px';
cvs.width = W * dpr; cvs.height = H * dpr;
ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.scale(dpr, dpr);
}
function loop() {
ctx.clearRect(0, 0, W, H);
areas.forEach(function(a) { ctx.fillStyle = a.bg; ctx.fillRect(a.x, a.y, a.w, a.h); ctx.drawImage(a.cc, a.ix, a.iy, a.iw, a.ih); });
for (let i = 0; i < pts.length; i++) {
const p = pts[i];
if (mouse.x !== null) {
const dx = p.x - mouse.x, dy = p.y - mouse.y, dist = Math.sqrt(dx*dx + dy*dy);
if (dist < mouse.r) {
const ang = Math.atan2(dy, dx) + (Math.random() - 0.5) * 0.8, f = (mouse.r - dist) / mouse.r;
p.vx += Math.cos(ang) * f * p.sp; p.vy += Math.sin(ang) * f * p.sp;
}
}
p.x += p.vx; p.y += p.vy; p.vx *= 0.94; p.vy *= 0.94;
p.x += (p.bx - p.x) * 0.025; p.y += (p.by - p.y) * 0.025;
if (p.x < p.bnd.x0) { p.x = p.bnd.x0; p.vx *= -0.5; } else if (p.x + 2 > p.bnd.x1) { p.x = p.bnd.x1 - 2; p.vx *= -0.5; }
if (p.y < p.bnd.y0) { p.y = p.bnd.y0; p.vy *= -0.5; } else if (p.y + 2 > p.bnd.y1) { p.y = p.bnd.y1 - 2; p.vy *= -0.5; }
p.mv = (Math.abs(p.x - p.bx) > 0.5 || Math.abs(p.y - p.by) > 0.5);
if (p.mv) {
// 方眼紙状の薄い影(アンチエイリアス隙間)を完全に消すため0.3pxずつ広げて塗りつぶす
ctx.fillStyle = '#fff'; ctx.fillRect(p.bx - 0.3, p.by - 0.3, 2.6, 2.6);
ctx.fillStyle = p.bg; ctx.fillRect(p.bx - 0.3, p.by - 0.3, 2.6, 2.6);
}
}
for (let i = 0; i < pts.length; i++) {
const p = pts[i];
if (p.mv) { ctx.fillStyle = p.c; ctx.fillRect(p.x, p.y, 2, 2); }
}
requestAnimationFrame(loop);
}
loop();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', startParticleApp);
} else {
startParticleApp();
}
})();
</script>


