【成图】




【代码】
<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<title>挖角矩形横幅 Draft1 欲买桂花同载酒,终不似,少年游。</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>
</div>
</body>
</html>
<script type="text/javascript">
<!--
/*****************************************************************
* 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中,
* 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。
******************************************************************/
// canvas的绘图环境
var ctx;
// 高宽
const WIDTH=512;
const HEIGHT=512;
// 舞台对象
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){
// 底色
ctx.save();
ctx.fillStyle = "white";
ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);
ctx.restore();
const R=240;// 基准尺寸
// 第1圈
ctx.save();
var r=R*1.00;
var w=2*r;
var h=w/2;
drawRect4Corner(ctx,0,0,w,h,R/10,"rgb(148,153,149)");
ctx.lineWidth=r/120;
ctx.strokeStyle = "rgb(148,153,149)";
drawUnfilledCornerRect(ctx,0,0,w,h,R/10);
ctx.stroke();
ctx.restore();
// 第2圈
ctx.save();
var r=R*1.00;
var w=2*r;
var h=w/2;
ctx.lineWidth=r/240;// 内圈
ctx.strokeStyle = "rgb(148,153,149)";
var gnt=ctx.createLinearGradient(-w/2,0,w/2,0);
gnt.addColorStop(0,"rgb(151,101,42)");
gnt.addColorStop(0.1,"rgb(180,131,64)");
gnt.addColorStop(0.25,"rgb(233,191,105)");
gnt.addColorStop(0.5,"rgb(245,230,135)");
gnt.addColorStop(0.75,"rgb(233,191,105)");
gnt.addColorStop(0.9,"rgb(180,131,64)");
gnt.addColorStop(1,"rgb(151,101,42)");
ctx.fillStyle = gnt;
drawUnfilledCornerRect(ctx,0,0,w-10,h-10,R/10);
ctx.fill();
ctx.stroke();
ctx.restore();
// 中间文字
ctx.save();
var color="rgb(31,36,39)";
var start=createPt(0,-r/15);
var gap=r/3.5;
var sz=R/6.6;
writeText(ctx,start.x,start.y,"欲买桂花同载酒",sz+"px 方正宋刻本秀楷简体",color);
writeText(ctx,start.x,start.y+gap,"终不似,少年游",sz+"px 方正宋刻本秀楷简体",color);
ctx.restore();
// 上下两线
ctx.save();
var color="rgb(31,36,39)";
ctx.strokeStyle=color;
ctx.beginPath();
ctx.moveTo(-r/2,-r*0.28);
ctx.lineTo(+r/2,-r*0.28);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(-r/2,+r*0.25);
ctx.lineTo(+r/2,+r*0.25);
ctx.stroke();
ctx.restore();
writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权
}
}
/*----------------------------------------------------------
函数:用于绘制缺角矩形
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
width:矩形宽
height:矩形高
cl:cornerLength 角边长
----------------------------------------------------------*/
function drawUnfilledCornerRect(ctx,x,y,width,height,cl){
ctx.beginPath();
ctx.moveTo(x+width/2,y);
ctx.lineTo(x+width/2,y-height/2+cl);
ctx.lineTo(x+width/2-cl,y-height/2+cl);
ctx.lineTo(x+width/2-cl,y-height/2);
ctx.lineTo(x-width/2+cl,y-height/2);
ctx.lineTo(x-width/2+cl,y-height/2+cl);
ctx.lineTo(x-width/2,y-height/2+cl);
ctx.lineTo(x-width/2,y+height/2-cl);
ctx.lineTo(x-width/2+cl,y+height/2-cl);
ctx.lineTo(x-width/2+cl,y+height/2);
ctx.lineTo(x+width/2-cl,y+height/2);
ctx.lineTo(x+width/2-cl,y+height/2-cl);
ctx.lineTo(x+width/2,y+height/2-cl);
ctx.closePath();
}
/*----------------------------------------------------------
函数:用于绘制缺角矩形的四个角方块,是drawUnfilledCornerRect的配套函数
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
width:矩形宽
height:矩形高
cl:cornerLength 角边长
fillStyle:填充色
----------------------------------------------------------*/
function drawRect4Corner(ctx,x,y,width,height,cl,fillStyle){
ctx.fillStyle=fillStyle;
ctx.fillRect(-width/2,-height/2,cl/2,cl/2);
ctx.fillRect(-width/2,height/2-cl/2,cl/2,cl/2);
ctx.fillRect(width/2-cl/2,-height/2,cl/2,cl/2);
ctx.fillRect(width/2-cl/2,height/2-cl/2,cl/2,cl/2);
}
/*----------------------------------------------------------
函数:用于绘制矩形
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
width:矩形宽
height:矩形高
----------------------------------------------------------*/
function drawRect(ctx,x,y,width,height){
ctx.beginPath();
ctx.moveTo(x-width/2,y-height/2);
ctx.lineTo(x+width/2,y-height/2);
ctx.lineTo(x+width/2,y+height/2);
ctx.lineTo(x-width/2,y+height/2);
ctx.closePath();
}
/*----------------------------------------------------------
函数:用于绘制实心圆,用途是标记点以辅助作图
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
r:圆半径
color:填充圆的颜色
----------------------------------------------------------*/
function drawSolidCircle(ctx,x,y,r,color){
ctx.fillStyle=color;
ctx.beginPath();
ctx.arc(x,y,r,0,Math.PI*2,false);
ctx.closePath();
ctx.fill();
}
/*----------------------------------------------------------
函数:创建一个二维坐标点
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>
END