【发布时间】:2019-12-13 23:34:02
【问题描述】:
我根据这个主题的主题提出了适合我需要的东西。
事实证明,为了缩放图像以进行图案填充,您需要 2 个画布。第一个画布创建在第二个画布中使用的缩放图像。我发现的所有示例都显示了两个画布,但我只想显示第二个画布
截图:
var c = document.getElementById("DrawQuote");
var ctx = c.getContext("2d");
ctx.clearRect(10, 10, c.width, c.height);
然后ctx上下文用来画一些矩形和多边形,我省略了代码
然后我需要用缩放的图像图案绘制一个矩形
截图:
// create the canvas with scaled image
var imgtoscale = new Image();
imgtoscale.src = "blah.jpg";
var canvas1 = document.getElementById("TempCanvas");
var ctx1 = canvas1.getContext("2d");
canvas1.width = imgtoscale.width / Scale / 4;
canvas1.height = imgtoscale.height / Scale / 4;
ctx1.drawImage(imgtoscale, 0, 0, canvas1.width, canvas1.height);
// draw a rectangle filled with the scaled image from above
// ctx is my main canvas, the one I want to display
ctx.fillStyle = ctx.createPattern(canvas1, "repeat");
ctx.fillRect(400, 40, 300, 300);
显示 2 张图片:
1-将图像缩放到 TempCanvas 元素
2-DrawQuote 元素具有缩放填充图案的矩形
但我只想显示 DrawQuote 元素
【问题讨论】: