Home > Web Front-end > HTML Tutorial > Color change of image drawn on HTML5 canvas element

Color change of image drawn on HTML5 canvas element

WBOY
Release: 2023-09-03 16:29:09
forward
667 people have browsed it

Color change of image drawn on HTML5 canvas element

In order to change the color of the image drawn on the HTML5 Canvas element, you can try running the following code. Use drawImage() method -

function display(img1, red, gr, bl) { //func to change color of image    
var canvas1 = document.createElement('canvas');//canvas element initialisation

canvas1.width = img.width;//canvas width initialisation    
canvas1.height = img.height; //canvas height initialisation

var ctx1 = canvas1.getContext('2d');    
ctx1.drawImage(img, 0, 0);

var myImg =ctx1.getImageData(0, 0, canvas1.width, canvas1.height);
for (var t=0;t< myImg.data.length;t+=4) {        
   myImg.data[t]= red | myImg.data[t];        
   myImg.data[t+1]= gr | myImg.data[t+1];        
   myImg.data[t+2]= bl | myImg.data[t+2];    
}
ctx1.putImageData(myImg,0,0); // Image data is adjusted according to context    
return c;
},
Copy after login

The above is the detailed content of Color change of image drawn on HTML5 canvas element. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template