码迷,mamicode.com
首页 > 其他好文 > 详细

Canvas 颜色反转

时间:2015-07-24 12:07:18      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

ImageData中的元素反转颜色 255-data[i,i+1,i+2]

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>converse color</title>
</head>

<body>
    <canvas id=‘myCanvas‘ width=‘800‘ height=‘600‘>
        your browser does not support canvas
    </canvas>
    <script type="text/javascript">
    var c = document.getElementById(myCanvas);
    var ctx = c.getContext(2d);
    var img = new Image();
    img.src = face.jpg;
    img.onload = function() {
        ctx.drawImage(img, 0, 0);
        var imgData = ctx.getImageData(0, 0, 250, 250);
        var pixels = imgData.data;
        for (var i = 0, n = pixels.length; i < n; i += 4) {
            pixels[i] = 255 - pixels[i];
            pixels[i + 1] = 255 - pixels[i + 1];
            pixels[i + 2] = 255 - pixels[i + 2];
        }
        ctx.putImageData(imgData, 250, 0);
    };
    </script>
</body>

</html>

 

Canvas 颜色反转

标签:

原文地址:http://www.cnblogs.com/stono/p/4672756.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!