css之canvas、 并使用canvas 实现写字效果。

1、什么是 Canvas?

HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像。

画布是一个矩形区域,您可以控制其每一像素。

canvas 拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。

创建 Canvas 元素
<canvas id="myCanvas" width="200" height="100"></canvas>
通过 JavaScript 来绘制
<script type="text/javascript">
var c=document.getElementById("myCanvas");
var cxt=c.getContext("2d");
cxt.fillStyle="#FF0000";
cxt.fillRect(0,0,150,75);
</script>

1.JavaScript 使用 id 来寻找 canvas 元素:

var c=document.getElementById("myCanvas");

2.创建 context 对象:

var cxt=c.getContext("2d"); 

demo:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<canvas id="root" width="300" height="200" style="border: 1px solid black;"></canvas>
<script>
<!--  获取canvas元素和绘图上下文-->
  const canvas = document.getElementById('root')
  const ctx = canvas.getContext('2d')
// 设置绘制样式
const fontSize = 40
ctx.font = `${fontSize}px Arial`
ctx.fillStyle = 'black'
// 要绘制的文字

const text = 'hello world'
const textWidth = ctx.measureText(text).width
const x = (canvas.width - textWidth) /2
const y = canvas.height /2

let currentTextLength = 0
let speed = 100

function drawText() {
  if(currentTextLength <= text.length){
    ctx.clearRect(0,0,canvas.width , canvas.height)
    const currentText = text.slice(0,currentTextLength)
    ctx.fillText(currentText,x,y)
    currentTextLength++
    requestAnimationFrame(drawText)
  }

}
drawText()

</script>

</body>
</html>

 

demo2:

<!DOCTYPE html>
<html>
<head>
  <title>Canvas Drawing Board</title>
</head>
<body>
<canvas id="drawingCanvas" width="800" height="400" style="border: 1px solid black;"></canvas>

<script>
  // 获取Canvas元素和绘图上下文
  const canvas = document.getElementById('drawingCanvas');
  const ctx = canvas.getContext('2d');

  // 设置绘制样式
  ctx.strokeStyle = 'black';
  ctx.lineWidth = 2;
  ctx.lineCap = 'round';
  ctx.lineJoin = 'round';

  // 记录鼠标是否按下的状态
  let isDrawing = false;

  // 记录鼠标位置
  let lastX = 0;
  let lastY = 0;

  // 监听鼠标按下事件
  canvas.addEventListener('mousedown', (e) => {
    isDrawing = true;
    [lastX, lastY] = [e.offsetX, e.offsetY];
  });

  // 监听鼠标移动事件
  canvas.addEventListener('mousemove', (e) => {
    if (isDrawing) {
      const currentX = e.offsetX;
      const currentY = e.offsetY;
      drawLine(lastX, lastY, currentX, currentY);
      [lastX, lastY] = [currentX, currentY];
    }
  });

  // 监听鼠标松开事件
  canvas.addEventListener('mouseup', () => {
    isDrawing = false;
  });

  // 监听鼠标离开Canvas事件
  canvas.addEventListener('mouseout', () => {
    isDrawing = false;
  });

  function drawLine(x1, y1, x2, y2) {
    ctx.beginPath();
    ctx.moveTo(x1, y1);
    ctx.lineTo(x2, y2);
    ctx.stroke();
  }
</script>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用HTML5中的Canvas元素和JavaScript来实现写字动画效果。具体实现步骤如下: 1. 创建一个Canvas元素,并设置它的宽度和高度。 2. 使用JavaScript获取Canvas的上下文对象。 3. 设置字体、颜色、阴影等样式属性。 4. 使用Canvas的beginPath()方法开始绘制路径。 5. 使用Canvas的moveTo()和lineTo()方法绘制字母路径。 6. 使用Canvas的stroke()方法绘制路径。 7. 使用JavaScript的定时器函数setInterval()不断更新字母的位置,从而实现动画效果。 下面是一个简单的示例代码: HTML代码: ```html <canvas id="myCanvas" width="500" height="200"></canvas> ``` JavaScript代码: ```javascript // 获取Canvas元素的上下文对象 var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); // 设置字体属性 ctx.font = "80px Arial"; ctx.fillStyle = "red"; ctx.shadowColor = "black"; ctx.shadowBlur = 10; // 定义字母路径 ctx.beginPath(); ctx.moveTo(50, 100); ctx.lineTo(100, 50); ctx.lineTo(150, 100); ctx.lineTo(200, 50); ctx.lineTo(250, 100); ctx.lineTo(250, 150); ctx.stroke(); // 定义定时器函数来更新字母位置 var x = 50; var y = 100; var dx = 5; var dy = -5; setInterval(function() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x+50, y-50); ctx.lineTo(x+100, y); ctx.lineTo(x+150, y-50); ctx.lineTo(x+200, y); ctx.lineTo(x+200, y+50); ctx.stroke(); x += dx; y += dy; if (x > canvas.width-200 || x < 0) { dx = -dx; } if (y > canvas.height-50 || y < 0) { dy = -dy; } }, 10); ``` 这段代码实现了一个不断移动的字母路径,可以根据需要修改字母的形状和动画效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值