Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="head"><b>Canvas Blur</b></div>
<div class="wrap">
	<canvas class="canvas blur"></canvas>
</div>

<button class="button" id="open">Open</button>
<hr />
<label for="blur_val">blur radius: </label>
<input class="para_input" type="number" value=6 min=1 id="blur_val" />
<button class="button" id="g_blur">G Blur</button>
<label for="blur_val_m">m blur scale: </label>
<input class="para_input" type="number" value=1 min=0 id="blur_val_m" />
<button class="button" id="m_blur">M Blur</button>
<hr />
<button class="button" id="recover">Recover</button>
              
            
!

CSS

              
                html,
body {
	padding: 0;
	margin: 0;
	font-family: Georgia, serif;
}

.head {
	height: 80px;
	line-height: 80px;
	margin: 0;
	padding: 0 15px;
	text-align: left;
	color: #666;
	font-size: 18px;
	user-select: none;
	position: relative;
}

.head:before {
	content: '';
	width: 7px;
	height: 70%;
	background: #30983F;
	position: absolute;
	top: 15%;
	left: 0;
}

.head b {
	font-size: 18px;
	color: #666;
}

.button {
	width: 90px;
	height: 35px;
	font-size: 16px;
	background:#E55C5C;
	color:#fff;
	border-radius:4px;
	border:none;
	outline:none;
	-webkit-tap-highlight-color: #999;
	margin:5px 10px;
}
.para_input{
	width: 50px;
	height:28px;
	font-size:16px;
	color:#666;
	padding:0 5px;
}
label{
	margin-left:10px;
}
.button:hover {
	background:#c05959;
}

.wrap {
	width: 100%;
	text-align:center;
}

.canvas {
	background: #f0f0f0;
	height: 400px;
	width: 100%;
}

@media screen and (max-width: 430px) {
	.head {
		height: 50px;
		line-height: 50px;
		padding: 10px 15px;
	}
	.head b {
		font-size: 14px;
		color: #666;
	}
}
              
            
!

JS

              
                var canvas_blur = document.querySelector(".canvas.blur");
var pixelRatio = window.devicePixelRatio || 1;
var c_w = parseInt(getComputedStyle(canvas_blur).width);
var c_h = parseInt(getComputedStyle(canvas_blur).height);
canvas_blur.width = c_w;
canvas_blur.height = c_h;

/******************************canvas blur class********************************/
class CanvasFastBlur {
	constructor(options) {
		this.blurRadius = options.blur || 3;
		
	}
	initCanvas(canvas){
		this.canvas = canvas;
		this.ctx = canvas.getContext('2d');
		let w = canvas.width;
		let h = canvas.height;
		this.canvas_off = document.createElement("canvas");
		this.ctx_off = this.canvas_off.getContext("2d");
		this.canvas_off.width = w;
		this.canvas_off.height = h;
		this.ctx_off.drawImage(canvas, 0, 0);
	}
	recoverCanvas(){
		let w = this.canvas_off.width;
		let h = this.canvas_off.height;
		this.canvas.width = w;
		this.canvas.height = h;
		this.ctx.drawImage(this.canvas_off,0,0);
	}
	gBlur(blur) {
		let start = +new Date();
		blur = blur || this.blurRadius;
		let canvas = this.canvas;
		let ctx = this.ctx;
		
		let sum = 0;
		let delta = 5;
		let alpha_left = 1 / (2 * Math.PI * delta * delta);
		let step = blur < 3 ? 1 : 2;
		for (let y = -blur; y <= blur; y += step) {
			for (let x = -blur; x <= blur; x += step) {
				let weight = alpha_left * Math.exp(-(x * x + y * y) / (2 * delta * delta));
				sum += weight;
			}
		}
		let count = 0;
		for (let y = -blur; y <= blur; y += step) {
			for (let x = -blur; x <= blur; x += step) {
				count++;
				ctx.globalAlpha = alpha_left * Math.exp(-(x * x + y * y) / (2 * delta * delta)) / sum * blur;
				ctx.drawImage(canvas,x,y);
			}
		}
		ctx.globalAlpha = 1;
		console.log("time: "+(+new Date() - start))
	}
	mBlur(distance){
		distance = distance<0?0:distance;
		console.log(distance);
		let w = this.canvas.width;
		let h = this.canvas.height;
		this.canvas.width = w;
		this.canvas.height = h;
		let ctx = this.ctx;
		ctx.clearRect(0,0,w,h);
		let canvas_off = this.canvas_off;
		
		for(let n=0;n<5;n+=0.1){
			ctx.globalAlpha = 1/(2*n+1);
			let scale = distance/5*n;
			ctx.transform(1+scale,0,0,1+scale,0,0);
			ctx.drawImage(canvas_off, 0, 0);
		}
		ctx.globalAlpha = 1;
		if(distance<0.01){
			window.requestAnimationFrame(()=>{
				this.mBlur(distance+0.0005);
			});
		}
	}
	
}
/*******************************************************************************/
var canvasBlur = new CanvasFastBlur({
	blur: 6
});
document.querySelector("#g_blur").addEventListener("click", function() {
	var blur = document.querySelector("#blur_val").value;
	canvasBlur.gBlur(blur);
});
document.querySelector("#m_blur").addEventListener("click", function() {
	var dis = document.querySelector("#blur_val_m").value/100;
	canvasBlur.mBlur(dis);
});
document.querySelector("#recover").addEventListener("click", function() {
	canvasBlur.recoverCanvas();
});
/***************************open image and draw to canvas*************************/
document.querySelector("#open").addEventListener("click", function() {
	canvas_blur.width = c_w;
	canvas_blur.style.width = c_w + "px";
	canvas_blur.height = c_h;
	canvas_blur.style.height = c_h + "px";
	openFile();
});

function openFile() {
	var input = document.querySelector("open_file");
	if(!input){
	    input = document.createElement('input');
		input.id = "open_file";
	}
	input.style.display = "none";
	document.body.appendChild(input);
	input.addEventListener('change', readFile, false);
	input.type = 'file';
	input.accept = 'image/*';
	input.click();
}

function readFile() {
	var file = this.files[0]; //获取input输入的图片
	//判断是否图片,在移动端由于浏览器对调用file类型处理不同,虽然加了accept = 'image/*',但是还要再次判断
	if (!/image\/\w+/.test(file.type)) {
		console.log("image needed!");
		return false;
	}
	var reader = new FileReader();
	reader.readAsDataURL(file); //转化成base64数据类型
	reader.onload = function(e) {
		drawToCanvas(this.result);
	}
}

function drawToCanvas(imgData) {
	var context = canvas_blur.getContext('2d');
	var img = new Image();
	img.src = imgData;
	img.onload = function() {
		context.clearRect(0, 0, canvas_blur.width, canvas_blur.height);
		var img_w = img.width > canvas_blur.width ? canvas_blur.width : img.width;
		var img_h = img.height > canvas_blur.height ? canvas_blur.height : img.height;
		var scale = (img_w / img.width < img_h / img.height) ? (img_w / img.width) : (img_h / img.height);
		img_w = img.width * scale;
		img_h = img.height * scale;
		canvas_blur.style.width = img_w + "px";
		canvas_blur.style.height = img_h + "px";
		canvas_blur.width = img_w;
		canvas_blur.height = img_h;
		context.drawImage(img, 0, 0, img.width, img.height, (canvas_blur.width - img_w) / 2, (canvas_blur.height - img_h) / 2, img_w, img_h);
		canvasBlur.initCanvas(canvas_blur);
	}
}
/**********************************************************************************/
              
            
!
999px

Console