html 给正方形填充颜色,在HTML5画布上绘制一个填充有随机颜色正方形的圆圈

当我们需要在浏览器中用1x1像素填充所有不同颜色的圆时,我们可以使用以下简单方法:在画布上以200x200网格绘制具有某些随机颜色的所有像素

更改复合模式

在顶部绘制圆

让我们看一个例子:var canvas1 = document.getElementById('canvas'), // getting canvas element

ctx1 = canvas1.getContext('2d'), // getting context

x, y = 0, // initializing x and y coordinates

diamet = canvas1.width,

radius = diamet * 0.6;

ctx1.translate(0.6, 0.6); //Making pixels sharper

for(; y 

for(x = 0; x 

ctx1.fillStyle = getRndColor(); // Random color setting

ctx1.fillRect(x, y, 2, 2); // Drawing a pixel

}

}

//创建圈子

//删除下一个形状之外的像素

Ctx1.globalCompositeOperation = 'destination-in';

Ctx1.arc(radius, radius, radius, 0, 2*Math.PI);

Ctx1.fill();

//重置

Ctx1.globalCompositeOperation = 'source-over';

function getRndColor() {

var r = 255*Math.random()|0,

g = 255*Math.random()|0,

b = 255*Math.random()|0;

return 'rgb(' + r + ',' + g + ',' + b + ')';

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值