HTML 5深入浅出教学篇之十一

介绍

站在用户的角度思考问题,与客户深入沟通,找到云霄网站设计与云霄网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、网站设计、外贸营销网站建设、企业官网、英文网站、手机端网站、网站推广、申请域名、网络空间、企业邮箱。业务覆盖云霄地区。

HTML 5: 画布(canvas)之效果

填充色, 笔划色, 颜色值 | fillStyle, strokeStyle

剪裁 | clip()

渐变色 | createLinearGradient(), createRadialGradient(), CanvasGradient.addColorStop()

贴图的平铺模式 | createPattern()

阴影效果 | shadowOffsetX, shadowOffsetY, shadowBlur, shadowColor

全局 Alpha | globalAlpha

新颜色与画布当前已有颜色的合成方式 | globalCompositeOperation

保存画布上下文,以及恢复画布上下文 | save(), restore()

像素操作 | createImageData(), getImageData(), putImageData(), ImageData, CanvasPixelArray

示例

1、填充色, 笔划色, 颜色值 | fillStyle, strokeStyle
canvas/effect/color.html

 
 
 
  1.  
  2.  
  3.  
  4.     填充色, 笔划色, 颜色值 
  5.  
  6.  
  7.      
  8.         您的浏览器不支持 canvas 标签  
  9.      
  10.      
  11.     Demo 
  12.     清除画布 
  13.      
  14.         var ctx = document.getElementById('canvas').getContext('2d');  
  15.         function drawIt() {  
  16.             clearIt();  
  17.             /*  
  18.              * context.fillStyle - 指定填充色的颜色值  
  19.              *  
  20.              * context.strokeStyle - 指定笔划色的颜色值  
  21.              *  
  22.              * 颜色值示例如下:  
  23.              *   Color Name - "green"  
  24.              *   #rgb - "#0f0"  
  25.              *   #rrggbb = "#00ff00" 
  26.              *   rgb(0-255, 0-255, 0-255) - rgb(0, 255, 0)  
  27.              *   rgb(0.0%-100.0%, 0.0%-100.0%, 0.0%-100.0%) - rgb(0%, 100%, 0%)  
  28.              *   rgba(0-255, 0-255, 0-255, 0.0-1.0) - rgb(0, 255, 0, 1)  
  29.              *   rgba(0.0%-100.0%, 0.0%-100.0%, 0.0%-100.0%, 0.0-1.0) - rgb(0%, 100%, 0%, 1)  
  30.              */  
  31.             ctx.fillStyle = 'Red';  
  32.             ctx.strokeStyle = 'Green';  
  33.             ctx.lineWidth = 10;  
  34.             // 看看 fillStyle 颜色的示例和 strokeStyle 颜色的示例,以及先 stroke 再 fill 和先 fill 再 stroke 的区别  
  35.             ctx.beginPath();  
  36.             ctx.rect(20, 20, 100, 100);  
  37.             ctx.stroke();  
  38.             ctx.fill();  
  39.             ctx.beginPath();  
  40.             ctx.rect(140, 20, 100, 100);  
  41.             ctx.fill();  
  42.             ctx.stroke();  
  43.         }  
  44.         function clearIt() {  
  45.             ctx.clearRect(0, 0, 260, 140);  
  46.         }  
  47.      
  48.  
  49.  

2、剪裁 | clip()
canvas/effect/clip.html

 
 
 
  1.  
  2.  
  3.  
  4.     剪裁 
  5.  
  6.  
  7.      
  8.         您的浏览器不支持 canvas 标签  
  9.      
  10.      
  11.     Demo 
  12.     清除画布 
  13.      
  14.         var ctx = document.getElementById('canvas').getContext('2d');  
  15.         function drawIt() {  
  16.             clearIt();  
  17.             var img = new Image();  
  18.             img.onload = function () {  
  19.                 ctx.drawImage(img, 0, 0, 512, 512);  
  20.             }  
  21.             img.src = "http://www.w3.org/html/logo/downloads/HTML5_Logo_512.png";  
  22.             // img.src = "http://www.cnblogs.com/assets/html5_logo.png";  
  23.             /*  
  24.              * context.clip() - 只显示当前路径所包围的剪切区域  
  25.              */  
  26.             ctx.beginPath();  
  27.             ctx.arc(256, 256, 100, 0, Math.PI * 2, true);  
  28.             ctx.clip();  
  29.         }  
  30.         function clearIt() {  
  31.             ctx.clearRect(0, 0, 512, 512);  
  32.         }  
  33.      
  34.  
  35.  

3、渐变色 | createLinearGradient(), createRadialGradient(), CanvasGradient.addColorStop()
canvas/effect/gradient.html

 
 
 
  1.  
  2.  
  3.  
  4.     渐变色 
  5.  
  6.  
  7.      
  8.         您的浏览器不支持 canvas 标签  
  9.      
  10.      
  11.     Demo 
  12.     清除画布 
  13.      
  14.         var ctx = document.getElementById('canvas').getContext('2d');  
  15.         function drawIt() {  
  16.             clearIt();  
  17.             /*  
  18.              * context.createLinearGradient(xStart, yStart, xEnd, yEnd) - 创建线性渐变对象,并返回 CanvasGradient 类型的对象  
  19.              *   xStart, yStart - 线性渐变对象的起始点坐标  
  20.              *   xEnd, yEnd - 线性渐变对象的结束点坐标  
  21.              *  
  22.              * CanvasGradient.addColorStop(offset, color) - 新增一个渐变参考点  
  23.              *   offset - 渐变参考点的位置,0.0 - 1.0 之间。起始点为 0,结束点为 1  
  24.              *   color - 渐变参考点的颜色值   
  25.              */  
  26.             var linearGradient = ctx.createLinearGradient(50, 0, 50, 100);  
  27.             linearGradient.addColorStop(0, "red");  
  28.             linearGradient.addColorStop(0.5, "green");  
  29.             linearGradient.addColorStop(1, "blue");  
  30.             ctx.beginPath();  
  31.             ctx.arc(50, 50, 50, 0, 2 * Math.PI, true);  
  32.             ctx.fillStyle = linearGradient;  
  33.             ctx.fill();  
  34.             /*  
  35.              * context.createRadialGradient(xStart, yStart, radiusStart, xEnd, yEnd, radiusEnd) - 创建放射性渐变对象,并返回 CanvasGradient   
  36.              *   xStart, yStart - 放射性渐变对象的开始圆的圆心坐标  
  37.              *   radiusStart - 开始圆的半径  
  38.              *   xEnd, yEnd - 放射性渐变对象的结束圆的圆心坐标  
  39.              *   radiusEnd - 结束圆的半径  
  40.              */  
  41.             var radialGradient = ctx.createRadialGradient(150, 50, 0, 150, 50, 50);  
  42.             radialGradient.addColorStop(0, "red");  
  43.             radialGradient.addColorStop(0.5, "green");  
  44.             radialGradient.addColorStop(1, "blue");  
  45.             ctx.beginPath();  
  46.             ctx.arc(150, 50, 50, 0, 2 * Math.PI, true);  
  47.             ctx.fillStyle = radialGradient;  
  48.             ctx.fill();  
  49.         }  
  50.         function clearIt() {  
  51.             ctx.clearRect(0, 0, 200, 100);  
  52.         }  
  53.      
  54.  
  55.  

#p#

4、贴图的平铺模式 | createPattern()
canvas/effect/pattern.html

 
 
 
  1.  
  2.  
  3.  
  4.     贴图的平铺模式 
  5.  
  6.  
  7.      
  8.         您的浏览器不支持 canvas 标签  
  9.      
  10.      
  11.     Demo 
  12.     清除画布 
  13.      
  14.         var ctx = document.getElementById('canvas').getContext('2d');  
  15.         function drawIt() {  
  16.             clearIt();  
  17.             var img = new Image();  
  18.             img.onload = function () {  
  19.                 /*  
  20.                  * context.createPattern(image, repetitionStyle) - 指定贴图的平铺模式  
  21.                  *   image - 图像对象。除了支持 HTMLImageElement 外,还支持 HTMLCanvasElement 和 HTMLVideoElement  
  22.                  *   repetitionStyle - 平铺模式(无论任何平铺模式,首图均在画布的左上角)  
  23.                  *     no-repeat - 不平铺,只贴图一次,当然也是在画布的左上角  
  24.                  *     repeat-x - x 轴方向上平铺图片  
  25.                  *     repeat-y - y 轴方向上平铺图片  
  26.                  *     repeat - x 轴和 y 轴方向均平铺图片  
  27.                  */  
  28.  
  29.                 var pattern = ctx.createPattern(img, "no-repeat");  
  30.                 ctx.fillStyle = pattern;  
  31.                 ctx.strokeStyle = "blue";  
  32.                 ctx.beginPath();  
  33.                 ctx.rect(0, 0, 300, 300);  
  34.                 ctx.fill();  
  35.                 ctx.stroke();  
  36.                 pattern = ctx.createPattern(img, "repeat-x");  
  37.                 ctx.fillStyle = pattern;  
  38.                 ctx.strokeStyle = "blue";  
  39.                 ctx.beginPath();  
  40.                 ctx.rect(320, 0, 300, 300);  
  41.                 ctx.fill();  
  42.                 ctx.stroke();  
  43.                 pattern = ctx.createPattern(img, "repeat-y");  
  44.                 ctx.fillStyle = pattern;  
  45.                 ctx.strokeStyle = "blue";  
  46.                 ctx.beginPath();  
  47.                 ctx.rect(0, 320, 300, 300);  
  48.                 ctx.fill();  
  49.                 ctx.stroke();  
  50.                 pattern = ctx.createPattern(img, "repeat");  
  51.                 ctx.fillStyle = pattern;  
  52.                 ctx.strokeStyle = "blue";  
  53.                 ctx.beginPath();  
  54.                 ctx.rect(320, 320, 300, 300);  
  55.                 ctx.fill();  
  56.                 ctx.stroke();  
  57.             }  
  58.             img.src = "http://res2.windows.microsoft.com/resbox/en/Windows%207/main/4300ae64-546c-4bbe-9026-6779b3684fb9_0.png";  
  59.             // img.src = "http://www.cnblogs.com/assets/windows_logo.png";  
  60.         }  
  61.         function clearIt() {  
  62.             ctx.clearRect(0, 0, 620, 620);  
  63.         }  
  64.      
  65.  
  66.  

5、阴影效果 | shadowOffsetX, shadowOffsetY, shadowBlur, shadowColor
canvas/effect/shadow.html

 
 
 
  1.  
  2.  
  3.  
  4.     阴影效果 
  5.  
  6.  
  7.      
  8.         您的浏览器不支持 canvas 标签  
  9.      
  10.      
  11.     Demo 
  12.     清除画布 
  13.      
  14.         var ctx = document.getElementById('canvas').getContext('2d');  
  15.         function drawIt() {  
  16.             clearIt();  
  17.             /*  
  18.              * context.shadowOffsetX - 阴影相对于实体的水平偏移值  
  19.              * context.shadowOffsetY - 阴影相对于实体的垂直偏移值  
  20.              * context.shadowBlur - 阴影模糊程度。默认值是 0 ,即不模糊  
  21.              * context.shadowColor - 阴影的颜色  
  22.              */  
  23.             ctx.shadowOffsetX = 5;  
  24.             ctx.shadowOffsetY = 10;  
  25.             ctx.shadowBlur = 10;  
  26.             ctx.shadowColor = "rgba(0, 0, 255, 0.5)";  
  27.             ctx.beginPath();  
  28.             ctx.arc(120, 120, 100, 0, Math.PI * 2, true);  
  29.             ctx.stroke();  
  30.             ctx.fillRect(300, 300, 100, 100);  
  31.         }  
  32.         function clearIt() {  
  33.             ctx.clearRect(0, 0, 480, 480);  
  34.         }  
  35.      
  36.  
  37.  

6、全局 Alpha | globalAlpha
canvas/effect/globalAlpha.html

 
 
 
  1.  
  2.  
  3.  
  4.     全局 Alpha 
  5.  
  6.  
  7.      
  8.         您的浏览器不支持 canvas 标签  
  9.      
  10.      
  11.     Demo 
  12.     清除画布 
  13.      
  14.         var ctx = document.getElementById('canvas').getContext('2d');  
  15.         function drawIt() {  
  16.             clearIt();  
  17.             /*  
  18.              * context.globalAlpha - 设置全局的 alpha 值  
  19.              */  
  20.             ctx.globalAlpha = "0.5";  
  21.             ctx.fillStyle = "red";  
  22.             ctx.beginPath();  
  23.             ctx.rect(20, 20, 100, 100);  
  24.             ctx.fill();  
  25.         }  
  26.         function clearIt() {  
  27.             ctx.clearRect(0, 0, 140, 140);  
  28.         }  
  29.      
  30.  
  31.  

#p#

7、新颜色与画布当前已有颜色的合成方式 | globalCompositeOperation
canvas/effect/globalCompositeOperation.html

 
 
 
  1.  
  2.  
  3.  
  4.     新颜色与画布当前已有颜色的合成方式 
  5.  
  6.  
  7.     
 
  •      
  •         您的浏览器不支持 canvas 标签  
  •      
  •      
  •     改变 globalCompositeOperation 
  •     清除画布 
  •      
  •         var ctx = document.getElementById('canvas').getContext('2d');  
  •         var compositeOperationTypes = ['source-atop', 'source-in', 'source-out', 'source-over', 'destination-atop', 'destination-in', 'destination-out', 'destination-over', 'lighter', 'copy', 'xor'];  
  •         var index = 0;  
  •         function drawIt() {  
  •             clearIt();  
  •             ctx.fillStyle = "red";  
  •             ctx.beginPath();  
  •             ctx.rect(20, 20, 100, 100);  
  •             ctx.fill();  
  •  
  •             /*  
  •              * context.globalCompositeOperation - 设置新颜色与画布当前已有颜色的合成方式  
  •              *   source-atop - 保留已有颜色,然后绘制新颜色与已有颜色重合的部分  
  •              *   source-in - 绘制新颜色与已有颜色重合的部分,其余全透明  
  •              *   source-out - 绘制新颜色与已有颜色不重合的部分,其余全透明  
  •              *   source-over - 在已有颜色的前面绘制新颜色。默认值  
  •              *   destination-atop - 在已有颜色的后面绘制新颜色,然后保留已有颜色与新颜色重合的部分  
  •              *   destination-in - 保留已有颜色与新颜色重合的部分,其余全透明  
  •              *   destination-out - 保留已有颜色与新颜色不重合的部分,其余全透明  
  •              *   destination-over - 在已有颜色的后面绘制新颜色  
  •              *   lighter - 混合重叠部分的颜色  
  •              *   copy - 删除已有颜色,只绘制新颜色  
  •              *   xor - 透明化重叠部分的颜色  
  •              */  
  •             ctx.globalCompositeOperation = compositeOperationTypes[index];  
  •             document.getElementById("msg").innerHTML = ctx.globalCompositeOperation;  
  •             ctx.fillStyle = "blue";  
  •             ctx.beginPath();  
  •             ctx.rect(80, 80, 100, 100);  
  •             ctx.fill();  
  •             index++;  
  •             if (index >= compositeOperationTypes.length)  
  •                 index = 0;  
  •         }  
  •         function clearIt() {  
  •             ctx.clearRect(0, 0, 200, 200);  
  •             // source-over 是 context.globalCompositeOperation 的默认值  
  •             ctx.globalCompositeOperation = "source-over";  
  •         }  
  •      
  •  
  •  
  • 8、保存画布上下文,以及恢复画布上下文 | save(), restore()
    canvas/effect/save_restore.html

     
     
     
    1.  
    2.  
    3.  
    4.     保存画布上下文,以及恢复画布上下文 
    5.  
    6.  
    7.     
      单击“save and draw”一次,然后单击“restore and draw”三次
       
    8.      
    9.         您的浏览器不支持 canvas 标签  
    10.      
    11.      
    12.     save and draw 
    13.     restore and draw 
    14.      
    15.         var ctx = document.getElementById('canvas').getContext('2d');  
    16.         /*  
    17.          * save() - 将画布的上下文压入堆栈  
    18.          * restore() - 从堆栈中取一个画布的上下文,如果没有则什么都不做  
    19.          */  
    20.         function drawIt() {  
    21.             clearIt();  
    22.             ctx.strokeStyle = "red";  
    23.             ctx.fillStyle = "green";  
    24.             ctx.lineWidth = 5;  
    25.             ctx.save(); // 将画布的上下文压入堆栈,此时堆栈中有一个画布上下文  
    26.             drawRect1();  
    27.             ctx.strokeStyle = "blue";  
    28.             ctx.fillStyle = "yellow";  
    29.             ctx.lineWidth = 10;  
    30.             ctx.save(); // 将画布的上下文压入堆栈,此时堆栈中有两个画布上下文  
    31.             drawRect2();  
    32.         }  
    33.         function restoreIt() {  
    34.             clearIt();  
    35.             ctx.restore(); // 按后进先出的顺序从堆栈里取画布上下文,如果取不到则什么都不做  
    36.             drawRect1();  
    37.             drawRect2();  
    38.         }  
    39.         function drawRect1() {  
    40.             ctx.beginPath();  
    41.             ctx.rect(20, 20, 100, 100);  
    42.             ctx.stroke();  
    43.             ctx.fill();  
    44.         }  
    45.         function drawRect2() {  
    46.             ctx.beginPath();  
    47.             ctx.rect(140, 20, 100, 100);  
    48.             ctx.stroke();  
    49.    

      本文标题:HTML 5深入浅出教学篇之十一
      当前URL:http://www.shufengxianlan.com/qtweb/news32/406032.html

      网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

      广告

      声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

    猜你还喜欢下面的内容

    外贸网站建设知识

    分类信息网