|
|

楼主 |
发表于 2022-8-17 13:02
|
显示全部楼层
参考代码:(尺寸、文本等数据应根据需要调整)
- <style>
- #papa { margin: auto; width: 740px; height: 460px; display: grid; place-items: center; box-shadow: 3px 3px 20px #000; position: relative; }
- #canv { position: absolute; top: 100px; border: 1px solid; }
- </style>
- <div id="papa">
- <canvas id="canv" width="700" height="200"></canvas>
- </div>
- <script>
- (function() {
- let w = canv.width, h = canv.height;
- let ctx = canv.getContext('2d');
- let x = 10, y = 40, currentWidth, idx = 0;
- let outText = '欲把西湖比西子,淡妆浓抹总相宜。';
- ctx.font = 'bold 40px 黑体';
- ctx.fillStyle = '#ff0000';
- ctx.shadowOffsetX = 2;
- ctx.shadowOffsetY = 2;
- ctx.shadowBlur = 4;
- ctx.shadowColor = '#000';
- function drawText(text) {
- ctx.clearRect(0, 0, w, h);
- ctx.fillText(text,x,y);
- ctx.fill();
- }
- (function updateText() {
- let txtstr = outText.substring(0, idx);
- drawText(txtstr);
- idx ++;
- if(idx > outText.length) idx = 0;
- setTimeout(updateText, 400);
- })();
- })();
- </script>
复制代码
|
|