代码:
<canvas id="canv" width="800" height="300"></canvas>
<script>
var cw1 = canv.width, ch1 = canv.height, cw2 = 100, ch2 = 100;
var tcanv = document.createElement('canvas');
tcanv.width = cw2, tcanv.height = ch2;
var ctx = canv.getContext('2d'), tctx = tcanv.getContext('2d');
function flash() {
tctx.clearRect(0, 0, cw2, ch2);
for (var i = 0; i < ch2 / 20; i++) {
for (var j = 0; j < cw2 / 20; j++) {
tctx.save();
tctx.fillStyle = `#${Math.random().toString(16).substr(-6)}`;
var x = 20 * i + 10, y = 20 * j + 10, r = 6;
tctx.beginPath();
tctx.arc(x, y, r, 0, Math.PI*2);
tctx.fill();
tctx.restore();
}
}
ctx.clearRect(0, 0, cw1, ch1);
var pattern = tctx.createPattern(tcanv, 'repeat');
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, cw1, ch1);
/* 仅描边
ctx.strokeStyle = pattern;
ctx.lineWidth = 40;
ctx.strokeRect(0, 0, cw1, ch1);
*/
/* 动态
requestAnimationFrame(flash);
*/
};
flash();
</script>
|