简体   繁体   English

在画布背景或矩形中打一个洞

[英]Make a hole in canvas background or in a rect

I use fabric.js and I need a transparent rect to be on a canvas, but I need to use background.我使用fabric.js,我需要一个透明的矩形在画布上,但我需要使用背景。
The problem is that I need background to be transparent under the rect.问题是我需要背景在矩形下透明。

I've created a fiddle to illustrate my problem: http://jsfiddle.net/goooseman/5xLE4/2/ (I need background to be transparent under the square).我创建了一个小提琴来说明我的问题: http : //jsfiddle.net/goooseman/5xLE4/2/ (我需要背景在正方形下是透明的)。

I think that it is impossible to make a hole in a background, but we can use another rect as a background.我认为在背景中打洞是不可能的,但我们可以使用另一个矩形作为背景。 I've created another fiddle to show it: http://jsfiddle.net/goooseman/cNJwL/1/ I use this code to make background rect:我创建了另一个小提琴来展示它: http : //jsfiddle.net/goooseman/cNJwL/1/我使用这个代码来制作背景矩形:

var backgroundRect = new fabric.Rect({
    left: 0,
    top: 0,
    fill: 'red',
    width: canvas.width,
    height: canvas.height
});

But how can I make a hole in the background rect under the upper rect?但是我怎样才能在上矩形下的背景矩形上打一个洞呢?

Maybe this could help (as found in this question ).也许这会有所帮助(如在此问题中所见)。

You need to first, add the canvas background object to which you want to do the "hole".您需要首先添加要对其进行“洞”处理的画布背景对象。 Then, you create a new object (the cutter), and set the property globalCompositeOperation = 'destination-out' , and then you add it to the canvas.然后,您创建一个新对象(切割器),并设置属性globalCompositeOperation = 'destination-out' ,然后将其添加到画布中。

Just like this:就像这样:

var h = new fabric.Rect({width: canvas.width, height: canvas.height, fill: 'rgba(0,0,0,0.5)'})
var z = new fabric.Circle({radius: 100, top:100, left: 100, globalCompositeOperation: 'destination-out'})
canvas.add(h).add(z)

This way, you add first the global object, and then you add the object as a "cutter".这样,您首先添加全局对象,然后将该对象添加为“切割器”。 I think it will act as a cutter for everything, so if you have other objects "behind", they will be cut too (haven't tested it).我认为它可以作为所有东西的切割器,所以如果你在“后面”有其他物体,它们也会被切割(还没有测试过)。

Hope this helps!!希望这有帮助!!

If it is just a square that needs to be cut out, you can simply draw 4 squares around it with the background colour.如果它只是一个需要剪掉的正方形,你可以简单地在它周围画4个正方形,背景色。

fabric.StaticCanvas('canvas');
canvas.setHeight(300);
canvas.setWidth(300);

var bgrect = new fabric.Rect({
    left:0,
    top:0,
    fill: 'red',
    width: 100,
    height: 300,
});
canvas.add(bgrect);
bgrect = new fabric.Rect({
    left:200,
    top:0,
    fill: 'red',
    width: 100,
    height: 300,
});
canvas.add(bgrect);
canvas.add(bgrect);
bgrect = new fabric.Rect({
    left:100,
    top:0,
    fill: 'red',
    width: 100,
    height: 100,
});
canvas.add(bgrect);
bgrect = new fabric.Rect({
    left:100,
    top:200,
    fill: 'red',
    width: 100,
    height: 100,
});
canvas.add(bgrect);

See this fiddle看到这个小提琴

I know I am late, but this can be useful for some users who can look for an answer我知道我迟到了,但这对一些可以寻找答案的用户很有用

 drawRectangleWithHole(document.getElementById('canvas').getContext('2d'), "black", 50, 50, 200, 200, 50, 50, 100, 100) function drawRectangleWithHole(ctx, color, x, y, width, height, holeX, holeY, holeWidth, holeHeight) { ctx.fillStyle = color ctx.fillRect(x, y, holeX, height) ctx.fillRect(x, y, width, holeY) ctx.fillRect(x + holeX + holeWidth, y, width - (holeX + holeWidth), height) ctx.fillRect(x, y + holeY + holeHeight, width, height - (holeY + holeHeight)) }
 <canvas id="canvas" width="300" height="300"><canvas>

Note that the hole's X and Y coordinates are relative to the rectangle请注意,孔的 X 和 Y 坐标是相对于矩形的

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

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