【发布时间】:2017-04-14 08:53:06
【问题描述】:
我在尝试使用画布和 CSS 模糊过滤器实现的效果时遇到了一些问题。
基本上我需要一个 100% 的窗口高度和宽度的画布,可以擦除它以显示位于下方的元素。我正在使用模糊,因此形状/绘图看起来很模糊(这是必需的)。
我的问题是,尽管画布尺寸过大,但拐角处仍有透明边缘,显示下方的元素(即具有蓝色背景的主体)。我尝试了多个负边距/溢出黑客,但似乎无法绕过它?
我需要这个画布是屏幕的整个宽度,模糊,并且根本不裁剪,但也许这就是 CSS 过滤器的呈现方式,只有可见的内容?
(function() {
// make canvas larger than window
var largeWidth = window.innerWidth * 3;
var largeHeight = window.innerHeight * 3;
function createCanvas(parent, width, height) {
var canvas = {};
canvas.node = document.createElement('canvas');
canvas.context = canvas.node.getContext('2d');
canvas.node.width = largeWidth;
canvas.node.height = largeHeight;
parent.appendChild(canvas.node);
return canvas;
}
function init(container, width, height, fillColor) {
var canvas = createCanvas(container, 3000, 3000);
var ctx = canvas.context;
ctx.fillCircle = function(x, y, radius, fillColor) {
this.fillStyle = fillColor;
this.beginPath();
this.moveTo(x, y);
this.arc(x, y, radius, 0, Math.PI * 2, false);
this.fill();
};
ctx.clearTo = function(fillColor) {
ctx.fillStyle = fillColor;
ctx.fillRect(0, 0, width, height);
};
ctx.clearTo(fillColor || "#ddd");
canvas.node.onmousemove = function(e) {
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
var radius = 100; // or whatever
var fillColor = '#ff0000';
ctx.globalCompositeOperation = 'destination-out';
ctx.fillCircle(x, y, radius, fillColor);
};
canvas.node.onmousedown = function(e) {
canvas.isDrawing = true;
};
}
var container = document.getElementById('canvas');
init(container, largeWidth, largeHeight, '#fff');
})();
/* CSS: */
body {
background: blue;
}
#canvas {
z-index: 1;
top: 0;
left: 0;
position: fixed;
filter: blur(10px);
-webkit-filter: blur(10px);
}
<div id = "canvas"></div>
请看我的fiddle
谢谢
【问题讨论】:
-
需要去掉div的过滤器吗?
-
@KarthikSivakumar 我需要滤镜,以获得模糊效果
标签: javascript jquery html css canvas