HTML5 Canvas筆記——HTML5 Canvas繪圖圖形的拼接平鋪等

參考書目:《HTML5 Canvas核心技術 圖形、動畫與游戲開發(fā)》

<!DOCTYPE html>

<html>

<head>

? ? <title>2-6</title>

? ? <style>

? ? ? ? #canvas{

? ? ? ? ? ? background: #eeeeee;

? ? ? ? ? ? border: thin solid cornflowerblue;

? ? ? ? }

? ? ? ? #radios{

? ? ? ? ? ? padding: 10px;

? ? ? ? }

? ? </style>

</head>

<body>

? ? <div id="radios">

? ? ? ? <input type="radio" id="repeatRadio" name='patternRadio' checked/>repeat

? ? ? ? <input type="radio" id="repeatXRadio" name='patternRadio'/>repeat-x

? ? ? ? <input type="radio" id="repeatYRadio" name='patternRadio'/>repeat-y

? ? ? ? <input type="radio" id="noRepeatRadio" name='patternRadio'/>no repeat? ? ? ?

? ? </div>

? ? <canvas id="canvas" width="1000" height="1000">

? ? ? ? Canvas not supported

? ? </canvas>

? ? <script src="2-6.js"></script>

</body>

</html>

2-6.js

var canvas=document.getElementById('canvas'),

? ? context=canvas.getContext('2d'),

? ? repeatRadio=document.getElementById('repeatRadio'),

? ? noRepeatRadio=document.getElementById('noRepeatRadio'),

? ? repeatXRadio=document.getElementById('repeatXRadio'),

? ? repeatYRadio=document.getElementById('repeatYRadio'),

? ? image=new Image();

function fillCanvasWithPattern(repeatString) {

? ? var pattern = context.createPattern(image,repeatString);

? ? context.clearRect(0, 0, canvas.width,canvas.height);

? ? context.fillStyle=pattern;

? ? context.fillRect(0, 0, canvas.width,canvas.height);

? ? context.fill();

}

repeatRadio.onclick=function(e){

? ? fillCanvasWithPattern('repeat');

};

repeatXRadio.onclick=function(e){

? ? fillCanvasWithPattern('repeat-x');

};

repeatYRadio.onclick=function(e){

? ? fillCanvasWithPattern('repeat-y');

};

noRepeatRadio.onclick=function(e){

? ? fillCanvasWithPattern('no-repeat');

};

image.src='e87d1a0ac24fae64a31d338ec66d83f3.png';

image.onload=function(e){

? ? fillCanvasWithPattern('repeat');

}

效果如圖所示:


?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容