gpt4 book ai didi

javascript - Canvas 。绘图前如何获取背景像素

转载 作者:行者123 更新时间:2023-12-02 14:24:03 24 4
gpt4 key购买 nike

我为我的 map 应用程序创建了一个小插件。该插件向几何特征添加文本标签。看起来像这样:

enter image description here

在上面的屏幕上,您可以看到 map 、水平线串和文本标签。我使用 canvascanvas.getContext("2d") 和一堆标准函数(如 ctx.StrokeText)创建了这个标签>ctx.fillText 等。我现在面临的问题是屏幕上的线串是交互式的或可移动的,我希望我的标签也能移动。我不是在问我的问题的确切解决方案。我感兴趣的是如何获取背景像素(就在我的文本标签下方),以便我可以在“移动”或在新位置重新绘制标签之前恢复它们。如果您可以提供一个很小的示例,其中您有一些背景,然后绘制一些对象,然后“删除”它,那就太好了。

最佳答案

您可能想使用 context.getImageData 和 context.putImageData

假设您的 Canvas 的 ID 为“myCanvas”,调用 doDraw() 将导致黑色矩形在复杂的背景上闪烁。

首先,在doDraw()中绘制背景。然后,在drawRectangle()中捕获要被矩形覆盖的背景并将其保存在变量“imageData”中。然后在背景上绘制矩形。然后,1秒后,调用eraseRectangle(),并且调用putImageData()来替换背景。

在这个 fiddle 中:

https://jsfiddle.net/f3Luxcoc/

这是 JavaScript:

//coordinates of rectangle
var xp = 20;
var yp = 20;
var wp = 80;
var hp = 80;

//saved background image
var imageData = null;

function doDraw() {

var can = document.getElementById("myCanvas");
can.width = 500;
can.height = 500;
var context = can.getContext("2d");

//draw background contents

var image = getImage();
context.drawImage(image, 0, 0);
context.drawImage(image, 100, 0);
context.drawImage(image, 0, 100);
context.drawImage(image, 100, 100);

drawRectangle();
}

function drawRectangle() {

var can = document.getElementById("myCanvas");
var context = can.getContext("2d");

//capture background
imageData = context.getImageData(xp, yp, wp, hp);

//draw Rectangle
context.rect(xp, yp, wp, hp);
context.fill();

setTimeout(function() {
eraseRectangle();
}, 1000);
}

function eraseRectangle() {
var can = document.getElementById("myCanvas");
var context = can.getContext("2d");

context.putImageData(imageData, xp, yp);

setTimeout(function() {
drawRectangle();
}, 1000);
}

doDraw();

function getImage() {
var image1 = new Image(237, 110);
image1.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAO0AAABuCAMAAAD8t2TLAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAC1QTFRF////LVcpkauOZYdhrsStx9jF1uTV+//66PPn8vzy4uji7vft4O3f+P/38fPwC7dd6gAACdxJREFUeNrsXIti2yoMNYiHiSH//7kXCXD8AmTaddudta1zEht0QOhIgnSaHnnkkUceeeSRv1vszzRlv7OjL2hoUZEwTSHUNHIsoLYJ1oYBvE2tjnfu1Nw85DbNWCVAxysvgS7OgxEcR8vglfINtDJ1cxNuRaur7t1OTUfoSPtNM1GLKHqaBUk4aakBP+73plBC9XONrbP03opJWs3dG1FLoT6vVeqMtFeTBZFQkhYCbEILFS37es2Etq6W4up99dTSXUewmyvrduj8RFMaYauMNtD/8ro/sN+FdhlB2x3sEBJas5ujFZ2c/O61CGl0TKW/8E1oYfolaO0idpOVp3qdy5AuVrRzuqGy3hgG6H4J2jJHXWcmxNYGk6FGS07rXtrkp4plRzj6utklW353eA3DSzH0PnsflitXeyertiBV5FfioNyeJDTB1dSUthc4xL+z0q6n90A8gEzCoVxZYJSXH5BIsvgZtRN6w6tyBFJFq2kAeyFCmIbEah7lhn2AswEp87wrIQ0n2lLN/nSxm6+GCNf+j70GYoBjdvFMcryFZMlxM+DmCKRNdr4TIuhBtIHr35boOPTRUxxpSH/VM2a0S8eDqEG0qXnorwONftKeu1V5jhUPrWpqa3uD/0W0+fl+PHWIXfPcFkvOEQUXbe2+atD5PWhLKD/z0No9C5CbTsFyqMRPl5Zcd7oOoAnmS+vW2uwk+4Pi9JED14kmcjWy75MDeSk511gP/X5YTIsSk08eqRuEKRBx2v6gBNtMsB2PA4MP1NQsL1nEOmcWojq8pxcjzHcn1kCb3A7pc8u+OByI3UkypmuWoUULsERzx/lvxgjNG+rUxwlcdWetKF60n5MHX0lPs4sSnhRrBFTZr99Dmx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FcqXEel7RVsqsIQWpIR3vE3u0cI7vixo4893fOCdgBDaV8Qau3vH1RKHFH/zDf7TsQuTuvkBSya0cb2+1rlNlzSV6nJuhXqLeIF/t2hxY/qV0b5wJD9HChAtUGtCKvGmniEPHPMA2zeipZlMaMtlHS3OBBC2HtrNb19roP1JL5UsGbLJCble7ix5ixafQpWNME200QCmdyqOb9FuLVli7/gVsR+15FfZjcGVlS+rXoomReZFnu++QnvwUgXtb/FSf4b8bh7+MXmRy9b/CFpctP/M1GKcKvX0yCOPPPLI/0P+E2AAA7JN+/OU8bgAAAAASUVORK5CYII="

return image1;
}

关于javascript - Canvas 。绘图前如何获取背景像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38433975/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com