简体   繁体   English

在HTML5中从画布中删除图像

[英]Removing an image from a canvas in HTML5

there's an example, which loads 2 images: 有一个例子,加载2张图片:

    canvas = document.getElementById("canvas");
    ctx = canvas.getContext("2d");

    var img1 = new Image();
    img.src = "/path/to/image/img1.png";
    img.onload = function() {
      ctx.drawImage(img, 0, 0);
    };

    var img2 = new Image();
    img2.src = "/path/to/image/img2.png";
    img2.onload = function() {
      ctx.drawImage(img2, 100, 100);
    };

I need to remove(replace) img2 from canvas. 我需要从画布中删除(替换)img2。 What is the best way to do it? 最好的方法是什么?

I think maybe you misunderstand what a Canvas is. 我想也许你误解了Canvas是什么。

A canvas is essentially a 2 dimensional grid of pixels along an 'X' axis and a 'Y' axis. 画布本质上是沿着“X”轴和“Y”轴的2维像素网格。 You use the API to draw pixels onto that canvas, so when you draw an image you're basically drawing the pixels that make up that image onto your canvas. 您可以使用API​​在该画布上绘制像素,因此在绘制图像时,您基本上是将构成该图像的像素绘制到画布上。 The reason there is NO method that lets you just remove an image, is because the Canvas doesn't know there's an image there in the first place, it just see pixels. 没有方法可以让你删除一个图像的原因是因为Canvas首先不知道那里有一个图像,它只是看到像素。

This is unlike the HTML DOM (Document Object Model) where everything is a HTML element, or an actual 'thing' you can interact with, hook-up script events to etc. this isn't the case with stuff you draw onto a Canvas. 这与HTML DOM(文档对象模型)不同,HTML DOM(文档对象模型)中的所有内容都是HTML元素,或者您可以与之交互的实际“事物”,将脚本事件连接到等等。这与您在Canvas上绘制的内容不同。 When draw a 'thing' onto a Canvas, that thing doesn't become something you can target or hook into, it's just pixels. 当在画布上绘制一个“东西”时,那个东西不会成为你可以瞄准或挂钩的东西,它只是像素。 To get a 'thing' you need to represent your 'thing' in some way such as a JavaScript object, and maintain a collection of these JS objects somewhere. 要获得“事物”,您需要以某种方式表示您的“事物”,例如JavaScript对象,并在某处维护这些JS对象的集合。 This how how Canvas games work. 这个Canvas游戏如何运作。 This lack of a DOM-like structure for Canvas makes rendering very fast, but can be a pain for implementing UI elements that you can easily hook into and interact with, remove etc. For that you might want to try SVG. Canvas缺乏类似DOM的结构使得渲染速度非常快,但是实现可以轻松挂钩并与之交互,删除等的UI元素会很麻烦。为此您可能想要尝试SVG。

To answer your question, simply paint a rectangle onto your Canvas that covers up your image by using the same X/Y coords and dimensions you used for your original image, or try Pointy's solution. 要回答您的问题,只需在Canvas上绘制一个矩形,通过使用与原始图像相同的X / Y坐标和尺寸来覆盖您的图像,或者尝试使用Pointy的解决方案。 'Cover-up' is probably the wrong terminology, since you're actually replacing the pixels (there are no layers in Canvas). '掩盖'可能是错误的术语,因为你实际上正在替换像素(Canvas中没有图层)。

It's not clear what you want the canvas to show when the image is gone. 目前还不清楚当图像消失时你想要画布显示什么。 If you want it to be transparent, you could get the image data and fill it with transparent pixels: 如果您希望它是透明的,您可以获取图像数据并用透明像素填充它:

var img = ctx.createImageData(w, h);
for (var i = img.data.length; --i >= 0; )
  img.data[i] = 0;
ctx.putImageData(img, 100, 100);

where "w" and "h" would be the width and height of your original image. 其中“w”和“h”是原始图像的宽度和高度。

edit — if you just want another image there, why not just put one there? 编辑 - 如果你只想要另一张图像,为什么不把它放在那里呢? It will overwrite whatever pixels are there on the canvas. 它将覆盖画布上的任何像素。

您可以使用clearRect()函数清除图像区域。然后清除整个上下文,您可以使用以下方法清除图像区域:

ctx.clearRect(xcoordinate_of_img1,ycoordinate_of_img1,xcoordinate_of_img1 + img1.width ,ycoord_of_img1 +img1.height );

If what "Sunday Ironfoot" said is right, then the best way to remove an image is by drawing the images once again from scratch. 如果“Sunday Ironfoot”所说的是正确的,那么删除图像的最佳方法是从头开始再次绘制图像。 For this, you need to have an array of images and draw only the ones you use. 为此,您需要拥有一组图像并仅绘制您使用的图像。 For example, 例如,

function EmptyClass{};
var img=new Array();
img[0]=new EmptyClass;
img[0].i=new Image();
img[0].src="yourfile1.jpg";
img[0].enabled=true;

img[1]=new EmptyClass;
img[1].i=new Image();
img[1].src="yourfile2.jpg";
img[1].enabled=false;// <-------- not enabled, should not be drawn equivalent to removing


img[2]=new EmptyClass;
img[2].i=new Image();
img[2].src="yourfile3.jpg";
img[2].enabled=true;

for(var i=0;i<3;i++){
if(img[i].enabled)ctx.drawImage(img[i], 100, 100);
}

PS I am creating an engine for javascript canvas. PS我正在为javascript画布创建一个引擎。 Will post it within a week 将在一周内发布

Peace 和平

Unlike drawing things yourself, if you 'replace' THE image on a canvas, the old one is still there. 与自己绘制的东西不同,如果你'替换'画布上的图像,旧的仍然存在。

Canvas c2;
...
        if (null != Image2) {
            var ctx = c2.getContext("2d");
            ctx.clearRect(0, 0, c2.width, c2.height);
        }

You can erase an image by drawing the same image again, using a different globalCompositeOperation 您可以使用不同的globalCompositeOperation通过再次绘制相同的图像来擦除图像

ctx.globalCompositeOperation ="xor"
ctx.drawImage(img2, 100, 100);

See https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation 请参阅https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalCompositeOperation

Can you overlay canvas objects (I guess I should try before asking, you can -1 one me for being lazy). 你可以覆盖画布对象(我想在问之前我应该​​尝试,你可以让我一个人懒惰)。 I guess I'd be interested in have one canvas element as a background, and then another for a layer objects that pop in and out of view. 我想我有兴趣将一个canvas元素作为背景,然后另一个用于弹出和移出视图的图层对象。 Might be a little more efficient then having to redraw every image if one gets deleted or moved. 如果一个图像被删除或移动,则可能需要重新绘制每个图像。 I'll play around and see what I can find. 我会玩,看看我能找到什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM