国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

詳解HTML5 Canvas繪制時(shí)指定顏色與透明度的方法

2020-03-24 17:03:57
字體:
供稿:網(wǎng)友
指定顏色黑色是Canvas繪制的默認(rèn)色彩,要想換一種顏色的話,就得在實(shí)際畫之前指定顏色。JavaScript Code復(fù)制內(nèi)容到剪貼板
functiondraw(){ varcanvas=document.getElementById('c1'); if(!canvas||!canvas.getContext){returnfalse;} varctx=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中一樣,我們指定顏色的時(shí)候還可以帶一個(gè)alpha值(不過用的不多,IE9之前都不支持)。看代碼:JavaScriptJavaScript Code復(fù)制內(nèi)容到剪貼板
functiondraw(){ varcanvas=document.getElementById('c1'); if(!canvas||!canvas.getContext){returnfalse;} varctx=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(); }
結(jié)果就是下面這樣:
和上面的代碼基本沒變化,就是把rgb(r, g, b)變成了rgba(r, g, b, a)而已,a的值也是0~1,0表示完全透明,1則是完全不透明(所以alpha的值實(shí)際上是 不透明度 )。
全局透明globalAlpha
這個(gè)也是很簡單的一個(gè)屬性,默認(rèn)值為1.0,代表完全不透明,取值范圍是0.0(完全透明)~1.0。這個(gè)屬性與陰影設(shè)置是一樣的,如果不想針對全局設(shè)置不透明度,就得在下次繪制前重置globalAlpha??偨Y(jié)一下:基于狀態(tài)的屬性有哪些? globalAlpha globalCompositeOpeartion strokeStyle textAlign,textBaseline lineCap,lineJoin,lineWidth,miterLimit fillStyle font shadowBlur,shadowColor,shadowOffsetX,shadowOffsetY
我們通過一個(gè)代碼,來體驗(yàn)一下globalAlpha的神奇之處~JavaScript Code復(fù)制內(nèi)容到剪貼板
style body{background:url( ./images/bg3.jpg )repeat;} #canvas{border:1pxsolid#aaaaaa;display:block;margin:50pxauto;} /style /head body divid= canvas-warp canvasid= canvas 你的瀏覽器居然不支持Canvas?!趕快換一個(gè)吧??! /canvas /div script window.onload=function(){ varcanvas=document.getElementById( canvas ); canvas.width=800; canvas.height=600; varcontext=canvas.getContext( 2d ); context.fillStyle= #FFF ; context.fillRect(0,0,800,600); context.globalAlpha=0.5; for(vari=0;i i++){ varR=Math.floor(Math.random()*255); varG=Math.floor(Math.random()*255); varB=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 /htmlhtml教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 仪征市| 南召县| 芜湖县| 绥阳县| 漯河市| 莱州市| 桂阳县| 莱阳市| 三原县| 郸城县| 临桂县| 乌兰浩特市| 阿合奇县| 新邵县| 太白县| 琼中| 绍兴市| 红河县| 易门县| 定安县| 苍梧县| 海阳市| 临西县| 阳朔县| 交口县| 松潘县| 民县| 高邑县| 临泉县| 东乡| 星子县| 仙桃市| 台湾省| 怀远县| 泰兴市| 巴里| 马公市| 阳泉市| 颍上县| 芒康县| 望谟县|