JavaScript canvas實(shí)現(xiàn)鏡像圖片效果
本文實(shí)例為大家分享了JavaScript canvas實(shí)現(xiàn)鏡像圖片效果的具體代碼,供大家參考,具體內(nèi)容如下

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Using image</title>
<style type="text/css">
* {
/* margin: 0;
padding: 0; */
box-sizing: border-box;
}
canvas {
/* border-width: 1px;
border-color: #000000;
border-style: solid; */
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<canvas id="mirror"></canvas>
<div>
<input type="file" accept="image/*" />
</div>
<script type="text/javascript">
window.onload = (event) => {
main()
}
function main() {
const canvas = document.getElementById("canvas");
const mirror = document.getElementById("mirror");
const ctx = canvas.getContext("2d");
const mirrorCtx = mirror.getContext("2d");
const inputFile = document.querySelector("input[type=file]");
inputFile.onchange = (event) => {
const files = event.target.files;
if (files.length > 0) {
const file = files[0]; // First file
console.log(file);
const image = new Image();
image.src = URL.createObjectURL(file);
image.onload = function(event) {
// console.log(event, this);
URL.revokeObjectURL(this.src);
canvas.width = image.width;
canvas.height = image.height;
mirror.width = image.width;
mirror.height = image.height;
ctx.drawImage(image, 0, 0);
const emptyImageData = ctx.createImageData(canvas.width, canvas.height);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
// console.log(getPixel(imageData, 0, 0));
// console.log(getPixel(imageData, 0, 5));
// console.log(getPixel(imageData, 0, 9));
// console.log(getColor(imageData, 0, 0, "R"));
// console.log(getColor(imageData, 0, 5, "G"));
// console.log(getColor(imageData, 0, 9, "B"));
// console.log(imageData);
// const uint8ClampedArray = imageData.data;
// uint8ClampedArray.length = imageData.width * imageData.height * 4;
console.log(imageData.data[0]);
for(let h = 0; h < imageData.height; h++) {
for(let w = 0; w < imageData.width; w++) {
const pixel = getPixel(imageData, h, imageData.width - w - 1);
setPixel(emptyImageData, h, w, pixel);
}
}
mirrorCtx.putImageData(emptyImageData, 0, 0);
console.log(imageData, emptyImageData);
function getPixel(imageData, row, column) {
const uint8ClampedArray = imageData.data;
const width = imageData.width;
const height = imageData.height;
const pixel = [];
for(let i = 0; i < 4; i++) {
pixel.push(uint8ClampedArray[row * width * 4 + column * 4 + i]);
}
return pixel;
}
function setPixel(imageData, row, column, pixel) {
const uint8ClampedArray = imageData.data;
const width = imageData.width;
const height = imageData.height;
for(let i = 0; i < 4; i++) {
uint8ClampedArray[row * width * 4 + column * 4 + i] = pixel[i];
}
}
// function getColor(imageData, row, column, color) {
// const pixel = getPixel(imageData, row, column);
// switch(color) {
// case "R":
// return pixel[0];
// case "G":
// return pixel[1];
// case "B":
// return pixel[2];
// case "A":
// return pixel[3];
// }
// return null;
// }
}
}
}
}
</script>
</body>
</html>
參考:鏈接
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Redux中進(jìn)行異步操作(網(wǎng)絡(luò)請(qǐng)求)的示例方案
這篇文章主要介紹了Redux中進(jìn)行異步操作(網(wǎng)絡(luò)請(qǐng)求)的方案,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
javascript 拖放效果實(shí)現(xiàn)代碼
JavaScript擅長(zhǎng)于修改頁(yè)面中的DOM元素,但是我們使用JavaScript通常只是實(shí)現(xiàn)一些簡(jiǎn)單功能,例如實(shí)現(xiàn)圖片的翻轉(zhuǎn),網(wǎng)頁(yè)中的標(biāo)簽頁(yè),等等。這篇文章將向你展示如何在頁(yè)面中,對(duì)創(chuàng)建的元素實(shí)現(xiàn)拖放。2010-01-01
BootStrap Validator 版本差異問(wèn)題導(dǎo)致的submitHandler失效問(wèn)題的解決方法
這篇文章主要介紹了BootStrap Validator 版本差異問(wèn)題導(dǎo)致的submitHandler失效問(wèn)題的解決方法,下面通過(guò)本文給大家詳細(xì)說(shuō)明一下,需要的朋友可以參考下2016-12-12
js從10種顏色中隨機(jī)取色實(shí)現(xiàn)每次取出不同的顏色
昨天在做js 從10種顏色中隨機(jī)取色,并每次取出的顏色不同,具體的實(shí)現(xiàn)思路如下,感興趣的朋友可以參考下2013-10-10
JS實(shí)現(xiàn)HTML頁(yè)面中動(dòng)態(tài)顯示當(dāng)前時(shí)間完整示例
這篇文章主要介紹了JS實(shí)現(xiàn)HTML頁(yè)面中動(dòng)態(tài)顯示當(dāng)前時(shí)間,結(jié)合完整實(shí)例形式分析了JavaScript使用時(shí)間函數(shù)setTimeout及clearTimeout動(dòng)態(tài)顯示當(dāng)前時(shí)間相關(guān)操作技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2018-07-07
Javascript中定義方法的另類寫法(批量定義js對(duì)象的方法)
用了很多的Javascript框架,偶爾也會(huì)去看一下框架的源碼,經(jīng)常會(huì)看到這樣的代碼。2011-02-02
echarts使用中關(guān)于y坐標(biāo)軸無(wú)法正常顯示的問(wèn)題解決記錄
Echarts是由百度提供的數(shù)據(jù)可視化解決方案,下面這篇文章主要給大家介紹了關(guān)于echarts使用中關(guān)于y坐標(biāo)軸無(wú)法正常顯示的問(wèn)題解決記錄,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
細(xì)數(shù)JavaScript 一個(gè)等號(hào),兩個(gè)等號(hào),三個(gè)等號(hào)的區(qū)別
下面小編就為大家?guī)?lái)一篇細(xì)數(shù)JavaScript 一個(gè)等號(hào),兩個(gè)等號(hào),三個(gè)等號(hào)的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10

