我为地图应用程序创建了一个小插件。此插件将文本标签添加到几何要素。它看起来像这样:
在上面的屏幕上,您可以看到地图,水平线串和文字标签。我使用canvas
,canvas.getContext("2d")
和一些标准函数(如ctx.strokeText
,ctx.fillText
等创建了此标签。我现在面临的问题是屏幕上的线串是互动或可移动,我也想让我的标签移动。我不是在问我问题的确切解决方案。我感兴趣的是如何获得背景像素(在我的文字标签下方),以便我可以在我移动"之前恢复它们。或者在新地方重新标记标签。如果你可以提供一个很小的例子,你有一些背景,然后绘制一些对象,然后"删除"它会很棒。
答案 0 :(得分:2)
您可能希望使用context.getImageData和context.putImageData
假设您的画布具有id" myCanvas",调用doDraw()将导致黑色矩形在复杂背景上闪烁。
首先,在doDraw()中绘制背景。然后,在drawRectangle()中捕获要由矩形覆盖的背景并保存在变量" imageData"中。然后在背景上绘制矩形。然后,1秒后,调用eraseRectangle(),并通过调用putImageData()替换背景。
在这个小提琴中:
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,iVBORw0KGgoAAAANSUhEUgAAAO0AAABuCAMAAAD8t2TLAAAAGXRFWHRTb2Z0d2FyZQ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