html5 canvas图片渐变

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script>
window.onload = function ()
{
    var oc = document.getElementById(‘c1‘);
    var ogc = oc.getContext(‘2d‘);

    var yimg = new Image();
    yimg.onload = function ()
    {
        draw(this);
    }
    yimg.src = ‘img/5-5.jpg‘;

    function draw(obj)
    {
        oc.width = obj.width;
        oc.height = obj.height*2;

        ogc.drawImage(obj,0,0);
        var oimg = ogc.getImageData(0,0,obj.width,obj.height);
        var w = oimg.width;
        var h = oimg.height;

        var newimg = ogc.createImageData(obj.width,obj.height);
        for(var i = 0; i < h; i++)
        {
            for(var j = 0; j < w; j++)
            {
                var color = getXY(oimg,j,i)
                var result = [];
                result[0] = 255 - color[0];
                result[1] = 255 - color[1];
                result[2] = 255 - color[2];
                result[3] = 255*i/h;

                setXY(newimg,j,h-i,result);
            }
        }

        ogc.putImageData(newimg,0,obj.height);
    }

    function getXY(obj,x,y)
    {
        var w = obj.width;
        var h = obj.height;
        var d = obj.data;

        var color = [];

        color[0] = d[ 4*(y*w+x)];
        color[1] = d[ 4*(y*w+x) +1];
        color[2] = d[ 4*(y*w+x) +2];
        color[3] = d[ 4*(y*w+x) +3];

        return color;
    }

    function setXY(obj,x,y,color)
    {
        var w = obj.width;
        var h = obj.height;
        var d = obj.data;

        d[ 4*(y*w+x)] = color[0];
        d[ 4*(y*w+x) +1] = color[1];
        d[ 4*(y*w+x) +2] = color[2];
        d[ 4*(y*w+x) +3] = color[3];

    }
}
</script>
<style>
body{
    background:pink;
    }
#c1{
    background:white;
    }
</style>
</head>

<body>
<canvas id="c1" width="400" height="800"></canvas>
</body>
</html>
时间: 2024-10-25 01:04:03

html5 canvas图片渐变的相关文章

html5 Canvas颜色渐变(画图很重要)

如果我们想要给图形上色,有两个重要的属性可以做到:fillStyle 和 strokeStyle.    fillStyle = color    strokeStyle = color strokeStyle 是用于设置图形轮廓的颜色,而 fillStyle 用于设置填充颜色.color 可以是表示 CSS 颜色值的字符串,渐变对象或者图案对象.默认情况下,线条和填充颜色都是黑色(CSS 颜色值 #000000). 下面的例子都表示同一种颜色.   // 这些 fillStyle 的值均为 '

HTML5 canvas图片爆炸特效

这是一款基于html5 canvas的炫酷图片爆炸飞散特效js插件.该js插件当用鼠标点击图片时,图片会有玻璃窗被子弹击碎时的爆炸飞散效果,非常炫酷. 在线演示:http://www.htmleaf.com/Demo/201502151384.html 下载地址:http://www.htmleaf.com/html5/html5-canvas/201502151383.html

HTML5 Canvas ( 图片填充样式 ) fillStyle, createPattern

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas</title> <script type="text/javascript" src="../js/jQuery.js"></script> <style type="text/css">

HTML5 Canvas ( 图片绘制 转化为base64 )

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas</title> <script type="text/javascript" src="../js/jQuery.js"></script> <style type="text/css">

html5 canvas 填充渐变形状

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

HTML5 Canvas ( 线性渐变, 升级版的星空 ) createLinearGradient, addColorStop

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas</title> <script type="text/javascript" src="../js/jQuery.js"></script> <style type="text/css">

html5 canvas 水平渐变描边

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

html5 canvas 径向渐变2

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

html5 canvas图片翻转

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload = function () { var oc = document.getElementById('c1'); var ogc = oc.getContext('2d'); var yimg = new Imag