【关键点】
底图的平铺操作,要想横纵无接缝需要多次调试。
【成图】

【代码】
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<title>使用HTML5/Canvas绘制安布雷拉桌面</title>
<style type="text/css">
.centerlize{
margin:0 auto;
width:1200px;
}
</style>
</head>
<body οnlοad="init();">
<div class="centerlize">
<canvas id="myCanvas" width="12px" height="12px" style="border:1px dotted black;">
如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.
</canvas>
<img id="myImg" src="174.jpg" style="display:none;"/>
</div>
</body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/
// canvas的绘图环境
var ctx;
// 高宽
const WIDTH=1920;
const HEIGHT=1080;
// 舞台对象
var stage;
//-------------------------------
// 初始化
//-------------------------------
function init(){
// 获得canvas对象
var canvas=document.getElementById('myCanvas');
canvas.width=WIDTH;
canvas.height=HEIGHT;
// 初始化canvas的绘图环境
ctx=canvas.getContext('2d');
ctx.translate(WIDTH/2,HEIGHT/2);// 原点平移
// 准备
stage=new Stage();
stage.init();
// 开幕
animate();
}
// 播放动画
function animate(){
stage.update();
stage.paintBg(ctx);
stage.paintFg(ctx);
// 循环
if(true){
//sleep(100);
window.requestAnimationFrame(animate);
}
}
// 舞台类
function Stage(){
// 初始化
this.init=function(){
}
// 更新
this.update=function(){
}
// 画背景
this.paintBg=function(ctx){
ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏
}
// 画前景
this.paintFg=function(ctx){
// 用底图去平铺整个画布
var img=document.getElementById("myImg");
var r=100;
const N=Math.ceil(WIDTH/100);
const M=Math.ceil(HEIGHT/100);
for(var i=0;i<N;i++){
for(var j=0;j<M;j++){
ctx.drawImage(img,10,10,402,424,-WIDTH/2+r*i,-HEIGHT/2+r*j,2*r,2*r);
}
}
// 中心稍亮背景渐黑的半透明蒙版
var rgnt=ctx.createRadialGradient(0,0,0,0,0,WIDTH/2);
rgnt.addColorStop(0,"rgba(35,35,35,0.77)");
rgnt.addColorStop(1,"rgba(1,1,1,0.88)");
ctx.fillStyle=rgnt;
ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
// 八片红白相间伞叶
const radius=200;
for(var i=0;i<8;i++){
var start=i*Math.PI/4+Math.PI/8;
var ptStart=createPt(radius*Math.cos(start),radius*Math.sin(start));
var end=start+Math.PI/4;
var ptEnd=createPt(radius*Math.cos(end),radius*Math.sin(end));
var ptArc=createPt(ptStart.x+ptEnd.x,ptStart.y+ptEnd.y);
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(ptStart.x,ptStart.y);
ctx.arc(ptArc.x,ptArc.y,radius,end-Math.PI,start-Math.PI,true);
ctx.closePath();
ctx.fillStyle=(i%2==1)?"red":"white";
ctx.fill();
ctx.lineWidth=8;
ctx.lineJoin="round";
ctx.strokeStyle="black";
ctx.stroke();
}
writeText(ctx,0,280,"Umbrella Corporation","48px consolas","lightgreen");
writeText(ctx,0,330,"OUR BUSINESS IS LIFE ITSELF.","28px consolas","rgb(224,224,224)");
writeText(ctx,WIDTH/2-20,HEIGHT/2-5,"逆火原创","8px consolas","white");// 版权
}
}
/*----------------------------------------------------------
函数:创建一个二维坐标点
x:横坐标
y:纵坐标
Pt即Point
----------------------------------------------------------*/
function createPt(x,y){
var retval={};
retval.x=x;
retval.y=y;
return retval;
}
/*----------------------------------------------------------
函数:延时若干毫秒
milliseconds:毫秒数
----------------------------------------------------------*/
function sleep(milliSeconds) {
const date = Date.now();
let currDate = null;
while (currDate - date < milliSeconds) {
currDate = Date.now();
}
}
/*----------------------------------------------------------
函数:书写文字
ctx:绘图上下文
x:横坐标
y:纵坐标
text:文字
font:字体
color:颜色
----------------------------------------------------------*/
function writeText(ctx,x,y,text,font,color){
ctx.save();
ctx.textBaseline="bottom";
ctx.textAlign="center";
ctx.font = font;
ctx.fillStyle=color;
ctx.fillText(text,x,y);
ctx.restore();
}
/*-------------------------------------------------------------
苏联体制说穿了,就是由政府充当总地主和总资本家,豢养几千万干部,
建立成一个等级森严的特权社会,造就一个鱼肉百姓高高在上的干部阶级,
普通百姓则仍然处于被奴役统治、被残酷剥削的社会低位......
苏联没有输给美国或任何人,苏联是被苏联自己打败的。
--------------------------------------------------------------*/
//-->
</script>
【底图】
下面为上文用到的底图174.jpg
