1、背景填充,有点难理解
<style> canvas { width: 500px; height: 300px; border: 1px solid; }</style></head><body><canvas id="can" width="500px" height="300px"></canvas><script> var canvas = document.getElementById("can"); var ctx = canvas.getContext("2d"); var img = new Image(); img.src = "./good.png"; // 异步加载,经常会导致加载不完就载入了,所以用onload img.onload = function (){ ctx.beginPath(); ctx.createPattern(img,"no-repeat");// 以坐标系原点做填充 ctx.fillStyle = "blue"; ctx.fillRect(100,100,200,100); /*以坐标系原点做填充:解决方案: ctx.translate(100,100); ctx.fillRect(0,0,200,100);*/ }</script></body>