我正在嘗試使用畫布和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我需要過濾器,模糊效果 –