您好,登錄后才能下訂單哦!
小編給大家分享一下canvas中ImageData的使用方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
canvas的ImageData 對(duì)象
ImageData對(duì)象中存儲(chǔ)著canvas對(duì)象真實(shí)的像素?cái)?shù)據(jù),它包含以下幾個(gè)只讀屬性:
width
圖片寬度,單位是像素
height
圖片高度,單位是像素
data
Uint8ClampedArray類型的一維數(shù)組,包含著RGBA格式的整型數(shù)據(jù),范圍在0至255之間(包括255)。
創(chuàng)建一個(gè)ImageData對(duì)象
去創(chuàng)建一個(gè)新的,空白的ImageData對(duì)象,你應(yīng)該會(huì)使用createImageData() 方法。
var myImageData = ctx.createImageData(width, height);
上面代碼創(chuàng)建了一個(gè)新的具體特定尺寸的ImageData對(duì)象。所有像素被預(yù)設(shè)為透明黑。
得到場(chǎng)景像素?cái)?shù)據(jù)
為了獲得一個(gè)包含畫布場(chǎng)景像素?cái)?shù)據(jù)的ImageData對(duì)像,你可以用getImageData()方法:
var myImageData = ctx.getImageData(left, top, width, height);
這個(gè)方法會(huì)返回一個(gè)ImageData對(duì)象,它代表了畫布區(qū)域的對(duì)象數(shù)據(jù),此畫布的四個(gè)角落分別表示為(left, top), (left + width, top), (left, top + height), 以及(left + width, top + height)四個(gè)點(diǎn)。這些坐標(biāo)點(diǎn)被設(shè)定為畫布坐標(biāo)空間元素。
在場(chǎng)景中寫入像素?cái)?shù)據(jù)
你可以用putImageData()方法去對(duì)場(chǎng)景進(jìn)行像素?cái)?shù)據(jù)的寫入。
ctx.putImageData(myImageData, dx, dy);
dx和dy參數(shù)表示你希望在場(chǎng)景內(nèi)左上角繪制的像素?cái)?shù)據(jù)所得到的設(shè)備坐標(biāo)。
例如,為了在場(chǎng)景內(nèi)左上角繪制myImageData代表的圖片,你可以寫如下的代碼:
ctx.putImageData(myImageData, 0, 0); toDataURL 將canvas轉(zhuǎn)為 data URI格式
有如下< canvas>元素
<canvas id="canvas" width="5" height="5"></canvas> var canvas = document.getElementById("canvas"); var dataURL = canvas.toDataURL(); console.log(dataURL); // "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNby // blAAAADElEQVQImWNgoBMAAABpAAFEI8ARAAAAAElFTkSuQmCC" var fullQuality = canvas.toDataURL("image/jpeg", 1.0); // data:image/jpeg;base64,/9j/4AAQSkZJRgABAQ...9oADAMBAAIRAxEAPwD/AD/6AP/Z" var mediumQuality = canvas.toDataURL("image/jpeg", 0.5); var lowQuality = canvas.toDataURL("image/jpeg", 0.1); 應(yīng)用一:顏色選擇器 var img = new Image(); img.src = 'haorooms.jpg'; var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); img.onload = function() { ctx.drawImage(img, 0, 0); img.style.display = 'none'; }; var color = document.getElementById('color'); function pick(event) { var x = event.layerX; var y = event.layerY; var pixel = ctx.getImageData(x, y, 1, 1); var data = pixel.data; console.log(data); var rgba = 'rgba(' + data[0] + ',' + data[1] + ',' + data[2] + ',' + (data[3] / 255) + ')'; color.style.background = rgba; color.textContent = rgba; } canvas.addEventListener('mousemove', pick);
應(yīng)用二:視頻純色背景過(guò)濾
今天,我們用getImageData過(guò)濾掉純色背景。
let processor = { timerCallback: function() { if (this.video.paused || this.video.ended) { return; } this.computeFrame(); let self = this; setTimeout(function () { self.timerCallback(); }, 0); }, doLoad: function() { this.video = document.getElementById("video"); this.c1 = document.getElementById("c1"); this.ctx1 = this.c1.getContext("2d"); this.c2 = document.getElementById("c2"); this.ctx2 = this.c2.getContext("2d"); let self = this; this.video.addEventListener("play", function() { self.width = self.video.videoWidth / 2; self.height = self.video.videoHeight / 2; self.timerCallback(); }, false); }, computeFrame: function() { this.ctx1.drawImage(this.video, 0, 0, this.width, this.height); let frame = this.ctx1.getImageData(0, 0, this.width, this.height); let l = frame.data.length / 4; for (let i = 0; i < l; i++) { let r = frame.data[i * 4 + 0]; let g = frame.data[i * 4 + 1]; let b = frame.data[i * 4 + 2]; if (g > 100 && r > 100 && b < 43) frame.data[i * 4 + 3] = 0; } this.ctx2.putImageData(frame, 0, 0); return; } };
應(yīng)用三:圖片灰度和反相顏色
在這個(gè)例子里,我們遍歷所有像素以改變他們的數(shù)值。然后我們將被修改的像素?cái)?shù)組通過(guò)putImageData()放回到畫布中去。invert函數(shù)僅僅是去減掉顏色的最大色值255.grayscale函數(shù)僅僅是用紅綠和藍(lán)的平均值。你也可以用加權(quán)平均,例如x = 0.299r + 0.587g + 0.114b這個(gè)公式。
var img = new Image(); img.src = 'rhino.jpg'; img.onload = function() { draw(this); }; function draw(img) { var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0); img.style.display = 'none'; var imageData = ctx.getImageData(0,0,canvas.width, canvas.height); var data = imageData.data; var invert = function() { for (var i = 0; i < data.length; i += 4) { data[i] = 225 - data[i]; // red data[i + 1] = 225 - data[i + 1]; // green data[i + 2] = 225 - data[i + 2]; // blue } ctx.putImageData(imageData, 0, 0); }; var grayscale = function() { for (var i = 0; i < data.length; i += 4) { var avg = (data[i] + data[i +1] + data[i +2]) / 3; data[i] = avg; // red data[i + 1] = avg; // green data[i + 2] = avg; // blue } ctx.putImageData(imageData, 0, 0); }; var invertbtn = document.getElementById('invertbtn'); invertbtn.addEventListener('click', invert); var grayscalebtn = document.getElementById('grayscalebtn'); grayscalebtn.addEventListener('click', grayscale); }
應(yīng)用四-縮放和反鋸齒
var img = new Image(); img.src = 'haorooms.jpg'; img.onload = function() { draw(this); }; function draw(img) { var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0); img.style.display = 'none'; var zoomctx = document.getElementById('zoom').getContext('2d'); var smoothbtn = document.getElementById('smoothbtn'); var toggleSmoothing = function(event) { zoomctx.imageSmoothingEnabled = this.checked; zoomctx.mozImageSmoothingEnabled = this.checked; zoomctx.webkitImageSmoothingEnabled = this.checked; zoomctx.msImageSmoothingEnabled = this.checked; }; smoothbtn.addEventListener('change', toggleSmoothing); var zoom = function(event) { var x = event.layerX; var y = event.layerY; zoomctx.drawImage(canvas, Math.abs(x - 5), Math.abs(y - 5), 10, 10, 0, 0, 200, 200); }; canvas.addEventListener('mousemove', zoom); }
應(yīng)用五-canvas手繪并下載圖片
window.addEventListener('load', function(ev) { var sourceimage = document.querySelector('img'); var canvas = document.querySelector('canvas'); var link = document.querySelector('a'); var context = canvas.getContext('2d'); var mouseX = 0, mouseY = 0, width = 300, height = 300, mousedown = false; canvas.width = width; canvas.height = height; context.fillStyle = 'hotpink'; function draw(ev) { if (mousedown) { var x = ev.layerX; var y = ev.layerY; x = (Math.ceil(x / 10) * 10) - 10; y = (Math.ceil(y / 5) * 5) - 5; context.fillRect(x, y, 10, 5); } } var link = document.createElement('a'); link.innerHTML = '下載圖片'; link.href = "#"; link.download = "haorooms.png"; document.body.insertBefore(link, canvas); canvas.addEventListener('mouseover', function(ev) { document.body.classList.add('painted'); }, false); canvas.addEventListener('mousemove', draw, false); canvas.addEventListener('mousedown', function(ev) { mousedown = true; }, false ); canvas.addEventListener('mouseup', function(ev) { link.href = canvas.toDataURL(); mousedown = false; }, false ); } ,false);
以上是“canvas中ImageData的使用方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。