特定色调
黑色是Canvas绘图的默认设置颜色,要想换1种色调的话,就得在具体画以前特定色调。
特定绘图线的色调:
特定填充的色调:
看来看具体的事例:
JavaScript
JavaScript Code拷贝內容到剪贴板
- onload = function() {
- draw();
- };
- function draw() {
- var canvas = document.getElementById('c1');
- if ( ! canvas || ! canvas.getContext ) { return false; }
- var ctx = canvas.getContext('2d');
- ctx.beginPath();
- ctx.fillStyle = 'rgb(192, 80, 77)';
- ctx.arc(70, 45, 35, 0, Math.PI*2, false);
- ctx.fill();
- ctx.beginPath();
- ctx.fillStyle = 'rgb(155, 187, 89)';
- ctx.arc(45, 95, 35, 0, Math.PI*2, false);
- ctx.fill();
- ctx.beginPath();
- ctx.fillStyle = 'rgb(128, 100, 162)';
- ctx.arc(95, 95, 35, 0, Math.PI*2, false);
- ctx.fill();
- }
实际效果以下图:
特定全透明度
和一般的CSS中1样,大家特定色调的情况下还能够带1个alpha值(但是用的很少,IE9以前都不适用)。看编码:
JavaScript
JavaScript Code拷贝內容到剪贴板
- onload = function() {
- draw();
- };
- function draw() {
- var canvas = document.getElementById('c1');
- if ( ! canvas || ! canvas.getContext ) { return false; }
- var ctx = canvas.getContext('2d');
- ctx.beginPath();
- ctx.fillStyle = 'rgba(192, 80, 77, 0.7)';
- ctx.arc(70, 45, 35, 0, Math.PI*2, false);
- ctx.fill();
- ctx.beginPath();
- ctx.fillStyle = 'rgba(155, 187, 89, 0.7)';
- ctx.arc(45, 95, 35, 0, Math.PI*2, false);
- ctx.fill();
- ctx.beginPath();
- ctx.fillStyle = 'rgba(128, 100, 162, 0.7)';
- ctx.arc(95, 95, 35, 0, Math.PI*2, false);
- ctx.fill();
- }
結果便是下面这样:
和上面的编码基础没转变,便是把rgb(r, g, b)变为了rgba(r, g, b, a)罢了,a的值也是0~1,0表明彻底全透明,1则是彻底不全透明(因此alpha的值具体上是“不全透明度”)。
全局性全透明globalAlpha
这个也是很简易的1个特性,默认设置值为1.0,意味着彻底不全透明,赋值范畴是0.0(彻底全透明)~1.0。这个特性与黑影设定是1样的,假如不想对于全局性设定不全透明度,就得在下一次绘图前重设globalAlpha。
总结1下:根据情况的特性有哪些?
——globalAlpha
——globalCompositeOpeartion
——strokeStyle
——textAlign,textBaseline
——lineCap,lineJoin,lineWidth,miterLimit
——fillStyle
——font
——shadowBlur,shadowColor,shadowOffsetX,shadowOffsetY
大家根据1个编码,来体验1下globalAlpha的奇异的地方~
JavaScript Code拷贝內容到剪贴板
- <!DOCTYPE html>
- <html lang="zh">
- <head>
- <meta charset="UTF⑻">
- <title>全局性全透明</title>
- <style>
- body { background: url("./images/bg3.jpg") repeat; }
- #canvas { border: 1px solid #aaaaaa; display: block; margin: 50px auto; }
- </style>
- </head>
- <body>
- <div id="canvas-warp">
- <canvas id="canvas">
- 你的访问器竟然不适用Canvas?!赶紧换1个吧!!
- </canvas>
- </div>
-
- <script>
- window.onload = function(){
- var canvas = document.getElementById("canvas");
- canvas.width = 800;
- canvas.height = 600;
- var context = canvas.getContext("2d");
- context.fillStyle = "#FFF";
- context.fillRect(0,0,800,600);
-
- context.globalAlpha = 0.5;
-
- for(var i=0; i<=50; i++){
- var R = Math.floor(Math.random() * 255);
- var G = Math.floor(Math.random() * 255);
- var B = Math.floor(Math.random() * 255);
-
- context.fillStyle = "rgb(" + R + "," + G + "," + B + ")";
-
- context.beginPath();
- context.arc(Math.random() * canvas.width, Math.random() * canvas.height, Math.random() * 100, 0, Math.PI * 2);
- context.fill();
- }
- };
- </script>
- </body>
- </html>
运作結果:
是否十分的酷?终究有点造型艺术家的范儿了吧。