为了账号安全,请及时绑定邮箱和手机立即绑定

canvas里我想给制作的多个图形加图片背景,怎么实现

canvas里我想给制作的多个图形加图片背景,怎么实现

慕仙0369855 2016-03-15 17:34:00
<script type="text/javascript">var canvas=document.getElementById("myCanvas");var context=canvas.getContext("2d");var bags=[]var img=new Image(); img.src="img/redbag.png";window.onload=function(){ canvas.width=800; canvas.height=800; for(var i=0;i<3;i++){ var bag={x:Math.random()*canvas.width,y:Math.random()*canvas.height} bags[i]=bag; } draw(); canvas.addEventListener("mouseup",change)}function change(){ var x=event.clientX-canvas.getBoundingClientRect().left; var y=event.clientY-canvas.getBoundingClientRect().top; draw(x,y);}function draw(x,y){ context.clearRect(0,0,canvas.width,canvas.height) for(var i=0;i<bags.length;i++){ context.beginPath(); context.rect(bags[i].x,bags[i].y,60,60); var pat=context.createPattern(img,"repeat") context.fillStyle=pat; context.fill(); if(context.isPointInPath(x,y)){ context.fillStyle="red"; context.fill(); } }} </script>
查看完整描述

目前暂无任何回答

  • 0 回答
  • 0 关注
  • 3666 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信