我需要在没有临时 Canvas 的 HTML5 Canvas 上绘制以下图像:
使用临时 Canvas 很容易,因为我可以独立处理重叠,就像您在此处看到的那样:
<强> Check this jsFiddle 。
// init
var canvas = document.getElementById('canvas');
var tempCanvas = document.getElementById('tempCanvas');
var ctx = canvas.getContext('2d');
var tempCtx = tempCanvas.getContext('2d');
// draw circle function
var drawCircle = function( c, color ) {
ctx.beginPath();
ctx.arc( c.x, c.y, 50, 0, 2 * Math.PI, false );
ctx.fillStyle = color;
ctx.fill();
}
// draw overlap function
var drawOverlap = function( c1, c2, color ) {
tempCtx.clearRect( 0, 0, 300, 300 );
// first circle
tempCtx.globalCompositeOperation = 'source-over';
tempCtx.beginPath();
tempCtx.arc( c1.x, c1.y, 50, 0, 2 * Math.PI, false );
tempCtx.fillStyle = color;
tempCtx.fill();
// second circle
tempCtx.globalCompositeOperation = 'destination-in';
tempCtx.beginPath();
tempCtx.arc( c2.x, c2.y, 50, 0, 2 * Math.PI, false );
tempCtx.fill();
// draw on main canvas
ctx.drawImage( tempCanvas, 0, 0 );
}
// circle objects
var c1 = { x:100, y: 200 };
var c2 = { x:180, y: 200 };
var c3 = { x:140, y: 140 };
// draw background
ctx.beginPath();
ctx.rect( 0, 0, 300, 300 );
ctx.fillStyle = 'black';
ctx.fill();
// draw circles
drawCircle( c1, 'grey' );
drawCircle( c2, 'white' );
drawCircle( c3, 'white' );
// draw overlaps
drawOverlap( c1, c2, 'red' );
drawOverlap( c1, c3, 'blue' );
drawOverlap( c2, c3, 'blue' );
你知道如何在没有第二 block Canvas 的情况下绘制它吗?非常感谢。
编辑:
感谢@user13500,我解决了这个问题。仍然有丑陋的边框,但速度非常快:
最佳答案
哎呀,我头好痛。
这不是一个很好的解决方案。我想根本就没有。但如果我们不关心背景,我们就会得到这样的结果:
<罢工> Fiddle 更新到新版本如下。
给我们这个:
尝试仅使用 globalCompositeOperation
来解决此问题;-P
编辑:
好的。离开它几分钟,然后我们又来了。这次就这样的结果。红色圆圈周围仍然存在杂散线的问题:
虽然它可能不是您想要的,但它就在这里;-) @markE当涉及到该主题的权威时,处于另一个领域。
代码:
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var ct = [
'source-over', // 0
'source-in', // 1
'source-out', // 2
'source-atop', // 3
'destination-over', // 4
'destination-in', // 5
'destination-out', // 6
'destination-atop', // 7
'lighter', // 8
'darker', // 9
'copy', // 10
'xor' // 11
];
ctx.beginPath();
ctx.globalCompositeOperation = ct[0];
ctx.fillStyle = "#888";
ctx.arc(100,200,50,0,2*Math.PI);
ctx.fill();
ctx.beginPath();
ctx.globalCompositeOperation = ct[6];
ctx.fillStyle = "#fff";
ctx.arc(180,200,50,0,2*Math.PI);
ctx.fill();
ctx.beginPath();
ctx.globalCompositeOperation = ct[11];
ctx.fillStyle = "#f00";
ctx.arc(100,200,50,0,2*Math.PI);
ctx.fill();
ctx.beginPath();
ctx.globalCompositeOperation = ct[4];
ctx.fillStyle = "#888";
ctx.arc(100,200,50,0,2*Math.PI);
ctx.fill();
ctx.beginPath();
ctx.globalCompositeOperation = ct[9];
ctx.fillStyle = "#fff";
ctx.arc(180,200,50,0,2*Math.PI);
ctx.fill();
ctx.beginPath();
ctx.globalCompositeOperation = ct[11];
ctx.fillStyle = "#fff";
ctx.arc(140,140,50,0,2*Math.PI);
ctx.fill();
ctx.beginPath();
ctx.globalCompositeOperation = ct[4];
ctx.fillStyle = "#00f";
ctx.arc(140,140,50,0,2*Math.PI);
ctx.fill();
ctx.beginPath();
ctx.globalCompositeOperation = ct[4];
ctx.rect( 0, 0, 300, 300 );
ctx.fillStyle = '#000';
ctx.fill();
关于javascript - 在 Canvas 上绘制 3 个圆圈的重叠部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21788608/