JavaScript 文字张草
【摘要】
<!DOCTYPE html>
<html>
<head>
<title> New Document </title>
&l...
<!DOCTYPE html>
<html>
<head>
<title> New Document </title>
<meta charset="utf8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">
<meta name="Generator" content="EditPlus">
<meta name="Author" content="chy龙神">
<meta name="Keywords" content="">
<meta name="Description" content="">
<style>
body {
margin: 0px;
overflow: hidden;
background-color: white;
}
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
<script>
document.body.onload = function() {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
//待绘制的文字
var inputStr="大话西游\n之\n爱你一万年";
//画布水平方向的留白
var canvasHorizontalPadding = 40;
var doubleCanvasHorizontalPadding = canvasHorizontalPadding * 2;
//画布宽度减去两边留白区域,得到的文字宽度阈值
var fontWidthThreshold = canvas.width - doubleCanvasHorizontalPadding;
//卷毛草的最大数量
var tendrilMaxCount = 1000;
//正在生长的卷毛草
var tendrilArray = new Array();
//存放文字区域的坐标
var strPosition = [];
/*
时间戳(毫秒)
(1) Date.parse(new Date()) 得到秒*1000为时间戳
(2) (new Date()).valueOf() 得到毫秒时间戳
(3) new Date().getTime() 得到毫秒时间戳
*/
var timestamp = new Date().getTime();
/*
绘制颜色为body的背景颜色(注意:这里背景一定要设置,如果不设置默认为透明,设置为白色也可以)
element.style只能获取内嵌样式
window.getComputedStyle(element)可以获取元素的最终样式
*/
ctx.fillStyle = window.getComputedStyle(document.body).backgroundColor
console.log(window.getComputedStyle(document.body).backgroundColor);
//字体的大小差不多就是字体四线三各的总高度
var fontSize = 1;
console.log("initFontSize: " + fontSize)
var fontWidth = fontSize, lastFontWidth;
//获取文字最适合的大小
do {
fontSize++;
ctx.font = "normal " + fontSize + "px Arial";
lastFontWidth = fontWidth;
fontWidth = ctx.measureText(inputStr).width;
} while(fontWidth < fontWidthThreshold);
//还原之前一次的文字大小,并重新设置
fontSize--;
ctx.font = "normal " + fontSize + "px Arial";
//ctx.fillText给出的是绘制文字的左下角坐标(注意:它并不支持换行)
//ctx.fillText(inputStr, (canvas.width - lastFontWidth) / 2, fontSize);
//为了给fillText添加换行效果,需要进行如下处理
var inputArray = inputStr.split("\n");
inputArray[inputArray.length] = "chy龍神書";
var lastPosX = (canvas.width - lastFontWidth) / 2;
var lastPosY = fontSize;
for(var i=0;i<inputArray.length;i++,lastPosY+=fontSize) {
var newWidth = ctx.measureText(inputArray[i]).width;
if(lastPosX + newWidth > canvas.width) {
lastPosX = (canvas.width - lastFontWidth) / 2;
}
ctx.fillText(inputArray[i], lastPosX, lastPosY);
lastPosX += newWidth;
//在文字下方绘制基线
ctx.strokeStyle = "rgb(0,0,0)";
ctx.beginPath();
ctx.moveTo(0, lastPosY);
ctx.lineTo(canvas.width, lastPosY);
ctx.stroke();
}
console.log("canvas.width: " + canvas.width + ", fontSize: " + fontSize);
//草的最大长度
var tendrilMaxLen = fontSize/6;
var imgData = ctx.getImageData(0,0,canvas.width,canvas.height);
//canvas未绘制的区域默认是透明的
for(var x=0; x<imgData.width; x++) {
for(var y=0; y<imgData.height; y++) {
if(getPixel(imgData,x,y)[3] > 0) {
strPosition.push( [x,y] );
}
}
}
loop();
function update() {
//移除已经张到最大长度的草,维持卷毛草的数量在一个合理的范围内
for(var i=tendrilArray.length-1; i>0; i--) {
if(tendrilArray[i].isReachMax) {
tendrilArray.splice(i,1);
}
}
var currentTimestamp = (new Date()).valueOf();
var timestampDiff = currentTimestamp - timestamp;
timestamp = currentTimestamp;
//将时间间隔按照一定比例转化为概率
var probability = timestampDiff / 50000;
//如果用户长时间离开后回来,时间差会变得很大
if(probability > 0.005) {
probability = 0.005;
}
// add new tendrils
for(p in strPosition) {
//对于每一个点,一定的概率长出卷毛,这个概率与经过的时间间隔有关
if(Math.random() < probability) {
var t = new tendril();
t.init(strPosition[p][0],strPosition[p][1]);
tendrilArray.push ( t );
}
}
// grow actuals tendrils
if(tendrilArray.length > 0) {
for(t in tendrilArray) {
tendrilArray[t].grow(1.0, 1.0, 0.02);
}
}
//console.log(tendrilArray.length);
}
function render() {
for(var i=0; i<tendrilArray.length; i++) {
tendrilArray[i].render();
}
}
function loop() {
update();
render();
window.requestAnimationFrame(loop);
}
//获取图像指定位置的像素[r,g,b,a]
function getPixel(imgData, x, y) {
var offset = (x + y * imgData.width) * 4;
var r = imgData.data[offset+0];
var g = imgData.data[offset+1];
var b = imgData.data[offset+2];
var a = imgData.data[offset+3];
return [r,g,b,a];
}
//卷须植物
function tendril() {
this.init = function(x, y) {
this.x = x;
this.y = y;
//[-PI,PI],随机的生长角度
this.angle = Math.random() * 2 * Math.PI - Math.PI;
//角速度
this.v = 0;
//当前长度
this.length = 0;
//是否已经达到最大长度
this.isReachMax = false;
};
//grow(1.0, 1.0, 0.02);
/*
distance:每帧生长的长度
curl:对角速度进行微调
step:每帧改变的角速度,值越大卷曲度越高
*/
this.grow = function(distance, curl, step) {
if(this.length < tendrilMaxLen) {
//上次的位置
this._x = this.x;
this._y = this.y;
//当前的位置
//[0, 1, 0]
this.x += Math.cos(this.angle) * distance;
//[-1, 1]
this.y += Math.sin(this.angle) * distance;
//[-step/2, step/2]
this.v += Math.random() * step - step / 2;
this.v *= 0.9 + curl*0.1;
this.angle += this.v;
this.length++;
} else {
this.isReachMax = true;
}
};
this.render = function() {
if(this._x != undefined) {
var rate = this.length/tendrilMaxLen;
r = 8;
//颜色由黑边绿
g = parseInt(rate * 255);
b = 32;
ctx.beginPath();
//渐渐变得透明
ctx.strokeStyle="rgba("+r+","+g+","+b+","+(1-rate)+")";
ctx.moveTo(this._x,this._y);
ctx.lineTo(this.x,this.y);
ctx.stroke();
}
}
};
}
</script>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/chy555chy/article/details/78749918
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)