简体   繁体   English

将globalCompositeOperation与canvas元素之外的元素一起使用

[英]Use globalCompositeOperation with elements outside of canvas element

So I'm trying to use globalCompositeOperation on an object within a <canvas> element but my goal is to blend with an object outide of the canvas - a plain html markup element like a paragraph. 所以我想用globalCompositeOperation一个内的对象上<canvas>元素,但我的目标是与对象outide画布融为一体-就像一个段落纯HTML标记元素。

My end goal will be inverting the content on the page using difference like so 我的最终目标是使用类似的difference来反转页面上的内容 在此处输入图片说明

My existing code is below. 我现有的代码如下。 Is this even possible? 这有可能吗?

var canvas = document.getElementById('canvas');
window.onresize=function(){
  "use strict";
  var winMin = Math.min(window.innerWidth,window.innerHeight);
  canvas.width = winMin;
  canvas.height = winMin;
  var w = winMin / 3;
  var ctx = canvas.getContext('2d');
  ctx.globalCompositeOperation = 'multiply';
  ctx.globalAlpha = .5;
  //magenta
  ctx.fillStyle = 'rgb(255,0,255)';
  ctx.beginPath();
  ctx.arc(w, w, w, 0, Math.PI*2, true); 
  ctx.closePath();
  ctx.fill();
  //cyan
  ctx.fillStyle = 'rgb(0,255,255)';
  ctx.beginPath();
  ctx.arc(w*2, w, w, 0, Math.PI*2, true); 
  ctx.closePath();
  ctx.fill();

};
window.onresize();

Codepen: http://codepen.io/jeremypbeasley/pen/NqwGoO Codepen: http ://codepen.io/jeremypbeasley/pen/NqwGoO

The globalCompositeOperation blending operations define how pixels backed by the canvas element blend with fragments to be written to that backing. globalCompositeOperation混合操作定义画布元素支持的像素如何与要写入该支持的片段混合。 That has nothing to do with pixels that live in some other dimension of the web page, like the DOM. 这与存在于网页其他维度(例如DOM)中的像素无关。 Total rasterization of the canvas occurs and some other graphics system composites the pixels of the canvas onto the pixels of the rest of the web page. 画布会发生完全栅格化,并且某些其他图形系统会将画布的像素合成到网页其余部分的像素上。 Reflow of the web page could happen at any time, but that does not mean that the canvas would be re-rasterized, just re-composited, in which case the globalCompositeOperation s would have no effect and you wouldn't see the photo negative effect you desire. 网页的重排可能随时发生,但这并不意味着画布将被重新栅格化,而是被重新组合,在这种情况下, globalCompositeOperation不会起作用,并且您不会看到照片的负面影响你渴望。

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

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