English 中文(简体)
HTML5 Canvas GlobalCompositeOperation for overlaying gradients not Plus up to high concentrations?
原标题:HTML5 Canvas globalCompositeOperation for overlaying gradients not adding up to higher intensity?

目前在“ fix,我想知道,是否有任何人知道可以把以下效果带上<canvas>s 2d 。

  • I have a radial gradient from black (alpha 0.5) to transparent 40pixel radius. center of the radial gradient is at x=50, y=50
  • I have another radial gradient from black (alpha 0.5) to transparent, 40pixel radius. center of the radial gradient is at x=80, y=50

The two gradients are overlapping. My problem now is: the overlapping area gets added up together resulting in a higher alpha value than the radial gradients centers and thus showing wrong data (e.g. hotter areas in a heatmap because of those additions between the gradients)

Have a look at the following gist, by executing it in your console you can see the problem.

Expected behaviour would be: Darkest areas are the gradients centers, the overlapping area of the two gradients merges but doesn t add up.

After seeing that none of the globalCompositeOperations resulted in the expected behaviour I tried combinations of those operations. A way I thought it maybe would be possible was the following:

  • draw first gradient
  • use compositeOperation destination-out
  • draw second gradient -> substracts overlapping area from the first gradient
  • use compositeOperation source-over
  • draw second gradient again

但不幸的是,我没有找到一个能够发挥作用的组合。 我很想事先听取你们的反馈意见!

PS:我知道,可以通过人工操纵餐厅来做到这一点,但我很想知道,这样做是否容易、更明智和更快。

最佳回答

这确实令人不安,但你想要做的事情却不忘形象。 所涉数据。

你们想到的是,在你驾stro他们时,你希望自己能够发挥的准确功能。 引证:

由于计算法如何界定一条途径,将一条中线运行中路段的重叠部分视为其结合是所描述的。

http://www.whatwg.org/specs/web-apps/present-work/multipage/the-canvas-element.html#trace-a-path” rel=“noreferer”>。

不管怎么说,你所希望的基本上是一条模糊的道路,只是你可以一劳永逸地回避的弧线,你完全能够取得你所期望的效果。

The only problem is that there is no way to make a blurry path in canvas. Or almost no way.

我们不使用这条道路,而是可以使用<>路面<>的影子,以模拟服从同样规则的bl圈。

这里的唯一问题是,你不想看到实际的道路,你只是想看到这条道路的阴影。 使中风透明化赢得了以下工作: 影子只能从不言而喻的角度看待。

但是,影子确实有以下特性:shadowOffsetXshadowOffsetY,这些特性通常被用来用一个钢子或两个钢子来改变阴影,从而造成对光源的幻觉。

但是,如果我们从现在的阴影中汲取什么东西,你会看到它们? 或者,如果我们选取paths的话。 你们能够看到他们,你们都能看到什么是阴影?

确实如此做。 这里迅速取得了成果,你原来的法典处于顶端,第二个阴影是:

“entergraph

这不是你以前在梯度和大小方面所做的事,而是非常接近,我相信,通过干涉你的价值观,你会变得更加接近。 两.。 日志确认,我们想要的一件事,一个不超过124(255人中有245人)的甲型字母正正确出现在它原来使用的143和134处的地方。

The fiddle to see the Code in action:

So there you have it. Getting the effect of the union of two radial gradients is possible without imageData if you use shadows and offset the actual paths so much that they are off the screen.

问题回答

I m从事以超5为基础的游戏,在这种游戏中,我想要将不同的半圆环带融为一体,从一个电网中抽取数百个广场。 影响像热图一样。 在进行了一些研究之后,我发现了Simon Sarris上文记载的“影子”技术。

采用这一技术是我所希望的。 我也认为很容易理解。 然而,在实践中,我发现,与我先前的提炼数千个填充物的技术(无论多么没有吸引力)相比,甚至使少数(约150)阴影变得更慢。

因此,我决定作一些分析。 我撰写了一些基本的 Java本(可在上看到的经修改的版本),以便从1250x600 canvas的五个不同类型中提取2000年版本,在五种主要桌面浏览器和移动式观测台的最新版本中记录这五项业务的历时。 (Sorry, desktop Sato)。 我也没有什么灵丹妙药来测试。 然后,我尝试了不同的效果组合,记录了过去的时间。

这里是一个简化的例子,说明Im 如何绘制两个梯度,其外观与影子相类似:

var gradient1 = context.createRadialGradient(75,100,2,75,100,80);
gradient1.addColorStop(0,"yellow");
gradient1.addColorStop(1,"black");

var gradient2 = context.createRadialGradient(125,100,2,125,100,80);
gradient2.addColorStop(0,"blue");
gradient2.addColorStop(1,"black");

context.beginPath();
context.globalCompositeOperation = "lighter";
context.globalAlpha = 0.5;
context.fillStyle = gradient1;
context.fillRect(0,0,200,200);
context.fillStyle = gradient2;
context.fillRect(0,0,200,200);
context.globalAlpha = 1.0;
context.closePath();

http://www.ohchr.org。

(2000年非重叠形状、全球沥青、梯度用于梯度,但不用于影子)

IE 11 (64-bit Windows 10)
 Rects     =   4 ms
 Arcs      =  35 ms
 Gradients =  57 ms
 Images    =   8 ms
 Shadows   = 160 ms

Edge (64-bit Windows 10)
 Rects     =   3 ms
 Arcs      =  47 ms
 Gradients =  52 ms
 Images    =   7 ms
 Shadows   = 171 ms

Chrome 48 (64-bit Windows 10)
 Rects     =   4 ms
 Arcs      =  10 ms
 Gradients =   8 ms
 Images    =   8 ms
 Shadows   = 203 ms

Firefox 44 (64-bit Windows 10)
 Rects     =   4 ms
 Arcs      =  21 ms
 Gradients =   7 ms
 Images    =   8 ms
 Shadows   = 468 ms

Opera 34 (64-bit Windows 10)
 Rects     =   4 ms
 Arcs      =   9 ms
 Gradients =   8 ms
 Images    =   8 ms
 Shadows   = 202 ms

Mobile Safari (iPhone5, iOS 9)
 Rects     =  12 ms
 Arcs      =  31 ms
 Gradients =  67 ms
 Images    =  82 ms
 Shadows   =  32 ms

OBSERVATIONS

  1. Among filled shapes, filled rects are consistently the fastest operation in all browsers and environments tested.
  2. Filled full arcs (circles) are about 10x slower in IE 11 and Edge than filled rects, compared to about 3.5x slower in the other major browsers.
  3. Gradients are roughly 3x slower than rects in IE 11, Chrome 48, and Opera 34, but a remarkable 100x slower in Firefox 44 (see Bugzilla report 728453).
  4. Images via drawImage() are roughly 1.5x as fast as filled rects in all desktop browsers.
  5. Shadowed filled arcs are slowest of all, ranging from around 50x slower than filled rects in IE, Edge, Chrome and Opera to 100x slower in Firefox.
  6. Chrome 48 and Opera 34 are both remarkably speedy in every category of shape except shadowed filled arcs, but they re no worse than other browsers there.
  7. Chrome and Opera crash when drawImage() draws 1000 shapes where either shadowOffsetX or shadowOffsetY is given a value outside the physical screen resolution.
  8. IE 11 and Edge are slower to paint arcs and gradients than other desktop browsers.
  9. drawImage() is slow on mobile Safari. It s actually faster to draw multiple gradients and shadowed arcs than to draw one copy many times with drawImage().
  10. Drawing in Firefox is sensitive to prior operations: drawing shadows and gradients makes drawing arcs slower. Fastest times are shown.
  11. Drawing in Mobile Safari is sensitive to prior operations: shadows make gradients slower; gradients and arcs make drawImage() even slower than it normally is. Fastest times are shown.

<>strong>ANALYSIS

虽然“影子”特征是一种简单和直观有效的混合方式,但比所有其他技术要慢得多。 这限制了其用途,而这种应用只需要汲取几个阴影,而不需要迅速和反复地利用许多阴影。 此外,在使用提款权时,投了阴影。 犯罪 X或影子 Y 价值大于3 000个 causes48和34号 Opera,几乎一分钟hang,耗时CPU周期,然后坠毁我的NVidia显示司机,甚至在更新后。 谷歌搜索发现,当一大笔阴影和 draw合使用时,没有用于 Ch的丑闻描述这一错误。

对于需要混合的异构体的用途,对影子采用最直观的类似做法是,将全球复合物作业设定为“灯光”并使用具有全球升值的提纲,以反复提取预设的半衰变体,或者在需要不同的颜色时提取单个梯度。 这并不完全与重叠的影子相匹配,而是紧密,避免进行每轮计算。 (然而,我们注意到,在流动游乐中,直接投影填满的弧体实际上比梯度和提纲更快。) 在为“灯光”而开展全球合作行动的同时,IE 11和Her在提炼弧方面比使用所有主要台式浏览器的影子更快,而且速度仅比在流动航道上填满的影子慢两倍。

http://www.ohchr.org。

If your only target platform is iPad/iPhone, the fastest method for nice-looking blended shapes is shadowed filled arcs. Otherwise, the fastest method with comparable appearance that I have found so far that works in all of the major desktop browsers is drawing radial gradients with globalCompositeOperation set to "lighter" and controlling opacity with globalAlpha.

<>说明: 在我进行的绘画测试中,有一些明显的改进方法。 尤其是,将每一形状的每一种情况都推向一个冲淡的缓冲地带,然后把整个缓冲带上可见的透镜,将大大改善业绩。 但是,这将否定这一检验的目标,即比较在可见的血管上绘制不同形状的相对速度。

http://jsfiddle.net/2qQLz/“rel=“nofollow” http://jsfiddle.net/2qQLz/是试图提供解决办法的。 如果接近你们的需要,可以进一步发展。 它把梯度限定在圈子交汇的一侧。 对于沿横向线的同一半径的两圈子,很容易找到圆圈交叉点的x价值,并为每一圈子的梯度填充提取束子。

两个任意的圈子比较困难,但是仍然可以找到交汇点,而且每个圈子仍然可以选择紧凑的试金。 随着更多的圈子的增加,它将逐渐变得更加复杂。

composite Mode 你们发现他们。 如我所知,你可以做比这更好的工作,而没有人工操作的

因此,每轮解决办法是什么?

我可以看到,将开始解决这一问题。

  1. 借助隐蔽的环境,并与手工操作相结合

  2. 撰写一项功能,以人工计算梯度,并使用定制混合功能。

你的第一项选择比第二项选择更具有多面性,因为你能够利用正常的血管方法,然后把这种血管vas倒在你的可见面孔上。 See @Phrogz context-blender 关于如何将一种环境融为一体的项目

第二种选择是必需的,因为你需要以血管不成事实为便利。

最大的困难是甲型的透明度,因为你可能含有rad梯。 一旦你掌握了背景形象,就几乎不可能看到你面前的画面。 即便是在每张纸张的基础上,你也遇到了困难:在另一个图像顶上的图像不会奏效。

从根本上说,这意味着,在你眼光的血管上,不管你选择通用的堆肥功能如何,把你对多种透光梯的图像 compos成一片,除非该信会具有透明的背景,否则就不然。

本着备选方案1的精神,你可以创造空白的环境,使多种梯度上去。 之后,情况就是如此。

本着选择2的精神,如果你能够确定点子的all,你就可以在单张通行证中计算这些点的形象和混合。

结合具有背景的单程送货技术,将允许你在可见的运河顶点上 draw光,而不必将单体照相读,只读纸浆。

我知道,在距离远的地方,这也许是实现我称之为甲型混血管对2D血管的影响的唯一真正途径。


I think the per pixel blend function you need would choose the pixel from the source and destination with the largest alpha value.

if (src.a <= dst.a) {
    result = dst;
} else {
    result = src;
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.