我正在尝试使用画布和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我需要过滤器,模糊效果 –