方法在圆下方留下一条线

方法在圆下方留下一条线

本文介绍了圆上使用的 HTML5 Canvas ctx.clip() 方法在圆下方留下一条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序来生成行星精灵.我这样做是通过创建一个圆形路径,运行 ctx.clip() 以将以下所有图层保留在圆圈内,然后绘制一个黑色和透明的纹理层,然后在上面绘制一个随机着色的矩形完整的画布,然后是阴影和发光.问题是裁剪后圆圈下方也出现一条彩色线,我不知道为什么.我需要删除这个.

I've created a program to generate planet sprites. I'm doing that by creating a circular path, running ctx.clip() to keep all the following layers inside of the circle, then drawing a black and transparent texture layer, then a randomly colored rectangle over the full canvas, then a shadow and glow on top of it all. The issue is that a colored line also appears under the circle after clipping, and I'm not sure why. I need this removed.

这是一个小提琴.最后一行将代码设置为每半秒循环一次:https://jsfiddle.net/tzkwmzqu/4/

Here is a fiddle. The last line sets the code to loop every half second: https://jsfiddle.net/tzkwmzqu/4/

推荐答案

我不确定我是否理解您的问题,但我假设您在谈论 抗锯齿 问题.

I am not sure I do understand your problem, but I will assume that you are talking about the anti-aliasing problem.

目前,您在剪切区域上绘制了很多内容.
在每次绘制时,新的抗锯齿效果都会使最新的绘制变得平滑.最后,本来应该是半透明的像素现在是完全不透明的.
另一方面,使用 globalCompositeOperation 就像 'destination-in',你只需要一张图就可以进行合成(~剪辑).所以你不会积累工件.但即使您这样做了,gCO 是全球性的,而且由于它考虑了透明度,因此积累就不那么重要了.

Currently, you are drawing a lot over your clipped area.
At each draw, new anti-aliasing artifacts will come to smooth the latest drawing. At the end, what should have been semi-transparent pixels are now fully opaque ones.
In the other hand, with globalCompositeOperation like 'destination-in', you need only one drawing to make the compositing (~clipping). So you don't accumulate artifacts. But even if you did, gCO is global and since it takes transparency into account, the accumulation would be less important.

var ctx1 = clip.getContext('2d');
var ctx2 = gCO.getContext('2d');
var ctx3 = gCO2.getContext('2d');

ctx1.beginPath();
ctx1.arc(150, 150, 150, 0, Math.PI*2)
ctx1.clip();
// drawing multiple times on this clipped area will increase artifacts
ctx1.fillRect(0,0,300, 150);
ctx1.fillRect(0,0,300, 150);
ctx1.fillRect(0,0,300, 150);
ctx1.fillRect(0,0,300, 150);

ctx2.beginPath();
ctx2.arc(150, 150, 150, 0, Math.PI*2)
ctx2.fillRect(0,0,300, 150);
ctx2.globalCompositeOperation = 'destination-in';
//With gCO you only draw once, but even if you did draw multiple times, there would still be less artifacts
ctx2.fill();
ctx2.fill();
ctx2.fill();
ctx2.fill();
ctx2.globalCompositeOperation = 'source-over';

ctx3.beginPath();
ctx3.arc(150, 150, 150, 0, Math.PI*2)
ctx3.fillRect(0,0,300, 150);
ctx3.globalCompositeOperation = 'destination-in';
// only one drawing needed:
ctx3.fill();
ctx3.globalCompositeOperation = 'source-over';

ctx1.fillStyle = ctx2.fillStyle = ctx3.fillStyle = "white";
ctx1.fillText('clipping', 120, 100);
ctx2.fillText('compositing', 120, 100);
ctx3.fillText('single compositing', 120, 100);
canvas{
  border: 1px solid;
  }
<canvas id="clip"></canvas><canvas id="gCO"></canvas><canvas id="gCO2"></canvas>

关于你的代码的一些不相关的注释:

A few unrelated notes about your code :

closePath 不会标记路径声明的结束,只有新的 beginPath() 调用会.ctx.fillStyle = '透明';ctx.fill() 不会做任何事情.只有putImageDataclearRect方法和globalCompositeOperation+绘制方法可以产生透明像素.

closePath does not mark the end of your path declaration, only a new beginPath() call does. ctx.fillStyle = 'transparent'; ctx.fill() won't do anything. Only putImageData, clearRect methods and globalCompositeOperation + drawing method can produce transparent pixels.

所以这里是一个片段中的所有内容:

So here is all the above in one snippet :

/* Load images */
var texture = new Image();
texture.src = "http://i.imgur.com/0qMwa8p.png";
var shadow = new Image();
shadow.src = "http://i.imgur.com/pX3HVFY.png";

/* Create the canvas and context references */
var canvas = document.getElementById("game");
canvas.style.width = (canvas.width = 512) + "px";
canvas.style.height = (canvas.height = 512) + "px";
var ctx = canvas.getContext("2d");

/* render */
function render() {
  /* Size of planets */
  var scale = Math.random() + 1


  // We don't need to save/restore the canvas state now,
  // simply remember to set the gCO back to 'source-over'
  // here it done at the end of the function

  /* Clear canvas for redraw */
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  /* Place texture onto planet */
  ctx.globalAlpha = Math.random() * .5 + .5;
  ctx.drawImage(texture, (Math.round(Math.random() * 256) - 128 * scale), (Math.round(Math.random() * 256) - 128 * scale), texture.naturalWidth * scale, texture.naturalHeight * scale)

  /* Color Planet */
  ctx.globalAlpha = 1;
  ctx.globalCompositeOperation = "multiply";
  var color = "hsl(" + Math.random() * 256 + ", 100%, 50%)"
  ctx.fillStyle = color;
  ctx.fillRect(0, 0, canvas.width, canvas.height)

  /* Give planet its shine and shadow */
  ctx.globalCompositeOperation = "source-over";
  ctx.drawImage(shadow, Math.round(Math.random() * 200 - 128 * scale), Math.round(Math.random() * 200 - 128 * scale), shadow.naturalWidth * scale, shadow.naturalHeight * scale)

  // instead of clipping, use gCO
  ctx.globalCompositeOperation = 'destination-in';
  ctx.beginPath();
  ctx.arc(256, 256, 128 * scale, 0, 2 * Math.PI);
  ctx.fill();
  // reset gCO
  ctx.globalCompositeOperation = 'source-over';
}
render()
window.interval = setInterval(render, 500)
#game {
  border: 1px solid black;
  background-color: black;
}
<canvas id="game"></canvas>

这篇关于圆上使用的 HTML5 Canvas ctx.clip() 方法在圆下方留下一条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 09:56