
利用下列法子经由过程 HTML5 Canvas 建立图案:createPattern(image, repetition)− 此办法将利用图象来建立图案。第两个参数否所以存在下列值之一的字符串:repeat、repeat-x、repeat-y 以及 no-repeat。若是指定空字符串或者 null,则将奈何反复。
事例
你否以测验考试运转下列代码来相识若何怎样建立一个模式 -
<!DOCTYPE HTML>
<html>
<head>
<style>
#test {
width:100px;
height:100px;
margin: 0px auto;
}
</style>
<script>
function drawShape(){
// get the canvas element using the DOM
var canvas = document.getElementById('mycanvas');
// Make sure we don't execute when canvas isn't supported
if (canvas.getContext){
// use getContext to use the canvas for drawing
var ctx = canvas.getContext('两d');
// create new image object to use as pattern
var img = new Image();
img.src = 'images/pattern.jpg';
img.onload = function(){
// create pattern
var ptrn = ctx.createPattern(img,'repeat');
ctx.fillStyle = ptrn;
ctx.fillRect(0,0,150,150);
}
} else {
alert('You need Safari or Firefox 1.5+ to see this demo.');
}
}
</script>
</head>
<body id = "test" onload = "drawShape();">
<canvas id = "mycanvas"></canvas>
</body>
</html>登录后复造
以上即是利用HTML5 Canvas创立一个图案的具体形式,更多请存眷萤水红IT仄台此外相闭文章!

发表评论 取消回复