forked from CodeLittlePrince/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2196ea7
commit 252c3d8
Showing
6 changed files
with
138 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
运行 | ||
--- | ||
因为canvas的getImageData会涉及跨域,所以,需要在本地启动一个服务器。 | ||
最方便的就是php起一个服务。 | ||
在命令行进入该文件夹目录,然后使用 `php -S 127.0.0.1:8888`就能启动服务,最后在游览器输入127.0.0.1:8888就能看到效果了。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
.box { | ||
float: left; | ||
width: 400px; | ||
} | ||
.box img { | ||
width: 100%; | ||
} | ||
|
||
canvas { | ||
float: left; | ||
width: 400px; | ||
} | ||
|
||
h3 { | ||
margin-bottom: 0; | ||
padding: 0; | ||
} | ||
|
||
.selection { | ||
padding: 0; | ||
margin-bottom: 20px; | ||
margin-top: 0; | ||
} | ||
|
||
.selection:after { | ||
display: table; | ||
content: ''; | ||
height: 0; | ||
visibility: hidden; | ||
clear: both; | ||
} | ||
.selection li { | ||
float: left; | ||
font-size: 16px; | ||
list-style: none; | ||
} | ||
.selection li button { | ||
outline: none; | ||
border: none; | ||
box-shadow: none; | ||
padding: 5px 40px; | ||
font-size: 16px; | ||
background-color: #333; | ||
color: #fff; | ||
margin-right: 20px; | ||
cursor: pointer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
<title>canvas 滤镜</title> | ||
<link rel="stylesheet" href="index.css"> | ||
</head> | ||
<body> | ||
<h3> | ||
滤镜选择: | ||
</h3> | ||
<ul class="selection"> | ||
<li> | ||
<button id="particle">粒子</button> | ||
</li> | ||
</ul> | ||
<div class="box"> | ||
<img id="img" src="index.png"> | ||
</div> | ||
<canvas id="canvas"></canvas> | ||
<script src="index.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
draw() | ||
|
||
function draw() { | ||
const canvas = document.getElementById('canvas') | ||
const ctx = canvas.getContext('2d') | ||
const img = document.getElementById('img') | ||
img.onload = function() { | ||
const style = window.getComputedStyle(img) | ||
const w = style.width | ||
const h = style.height | ||
const ws = w.replace(/px/, '') | ||
const hs = h.replace(/px/, '') | ||
canvas.width = ws | ||
canvas.height = hs | ||
ctx.drawImage(img, 0, 0, canvas.width, canvas.height) | ||
// 替换颜色 | ||
const originColor = ctx.getImageData(0, 0, ws, hs) | ||
const originColorData = originColor.data | ||
const output = ctx.createImageData(ws, hs) | ||
const outputData = output.data | ||
// 灰化按钮绑定 | ||
const particleBtn = document.getElementById('particle') | ||
particleBtn.addEventListener('click', function() { | ||
particle(originColorData, outputData, ws, hs) | ||
ctx.putImageData(output, 0, 0) | ||
}) | ||
} | ||
} | ||
|
||
// 粒子化 | ||
function particle(originColorData, outputData, ws, hs) { | ||
let index; | ||
let r, g, b; | ||
// 按行扫描 | ||
for (let y = 1; y <= hs; y++) { | ||
// 按列扫描 | ||
for (let x = 1; x <= ws; x++) { | ||
// rgb处理 | ||
index = ((y - 1) * ws + (x - 1)) * 4 | ||
outputData[index] = originColorData[index] | ||
outputData[index + 1] = originColorData[index + 1] | ||
outputData[index + 2] = originColorData[index + 2] | ||
// alpha | ||
outputData[index + 3] = 255; | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.