使用canvas实现雪花飘动效果


前言

四周山上的层层的松枝,戴着白绒般的很厚的雪,沉沉下垂,不时的掉下一两片手掌大的雪块,无声的堆在雪地上——冰心《寄小读者》

今天我们就使用canvas来实现雪花飘落的效果❄️


一、canvas是什么?

HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.

<canvas> 标签只是图形容器,您必须使用脚本来绘制图形。

你可以通过多种方法使用 canvas 绘制路径,盒、圆、字符以及添加图像。

二、canvas的基本用法

1.创建一个画布(Canvas)

<canvas id="myCanvas" width="200" height="100"></canvas>

2.使用JavaScript绘制图像

//首先找到<canvas>元素
var c=document.getElementById("myCanvas");
//然后创建context对象
var ctx=c.getContext("2d");
//下面的两行代码绘制一个红色的矩形:
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,150,75);

getContext("2d") 对象是内建的 HTML5 对象,拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。

设置fillStyle属性可以是CSS颜色,渐变,或图案。fillStyle 默认设置是#000000


3.Canvas 坐标

canvas 是一个二维网格。
canvas 的左上角坐标为 (0,0)
ctx.fillRect(0,0,150,75);
上面的 fillRect 方法拥有参数 (0,0,150,75)。
意思是:在画布上绘制 150x75 的矩形,从左上角开始 (0,0)。

4.Canvas - 路径

moveTo(x,y) 定义线条开始坐标
lineTo(x,y) 定义线条结束坐标
在canvas中绘制圆形, 我们将使用以下方法:

arc(x,y,r,start,stop)

使用arc() 画一个圆

var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();

三、实现雪花飘动的思路

1.创建一个画布(Canvas)

	var canvas =document.getElementById("canvas")
    //参数 contextID 指定了您想要在画布上绘制的类型。
    //当前唯一的合法值是 "2d",它指定了二维绘图,
    //并且导致这个方法返回一个环境对象,该对象导出一个二维绘图 API。
    var context = canvas
  • 10
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
1. 创建canvas画布 在uniapp的vue文件中,我们可以使用`<canvas>`标签来创建canvas画布。我们需要设置画布的宽高,以及给画布一个唯一的id。 ``` <canvas id="snow-canvas" canvas-id="snow-canvas" :style="{width: canvasWidth + 'px', height: canvasHeight + 'px'}"></canvas> ``` 在`data`中定义`canvasWidth`和`canvasHeight`,并在`mounted`生命周期函数中获取canvas的上下文。 ``` data() { return { canvasWidth: 0, canvasHeight: 0, ctx: null, snows: [] } }, mounted() { uni.getSystemInfo({ success: (res) => { this.canvasWidth = res.windowWidth this.canvasHeight = res.windowHeight const context = uni.createCanvasContext('snow-canvas', this) this.ctx = context } }) } ``` 2. 定义雪花类 我们可以定义一个雪花类,包含雪花的位置、大小、速度等属性,以及雪花的绘制方法。 ``` class Snow { constructor(canvasWidth, canvasHeight) { this.x = Math.random() * canvasWidth this.y = Math.random() * canvasHeight this.radius = Math.random() * 3 + 1 this.speed = Math.random() * 2 + 1 this.alpha = Math.random() * (1 - 0.5) + 0.5 } draw(ctx) { ctx.beginPath() ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false) ctx.fillStyle = `rgba(255, 255, 255, ${this.alpha})` ctx.fill() } update(canvasWidth, canvasHeight) { this.y += this.speed if (this.y > canvasHeight) { this.y = 0 this.x = Math.random() * canvasWidth } } } ``` 3. 在canvas中绘制雪花 在`mounted`生命周期函数中,我们可以循环创建多个雪花对象,并将它们存储在`snows`数组中。 ``` for (let i = 0; i < 100; i++) { const snow = new Snow(this.canvasWidth, this.canvasHeight) this.snows.push(snow) } ``` 在`drawSnows`方法中,我们可以循环遍历`snows`数组,对每个雪花对象进行绘制和更新。 ``` drawSnows() { this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight) for (let i = 0; i < this.snows.length; i++) { const snow = this.snows[i] snow.draw(this.ctx) snow.update(this.canvasWidth, this.canvasHeight) } uni.drawCanvas({ canvasId: 'snow-canvas', actions: this.ctx.getActions() }, this) } ``` 4. 实现动画效果 我们可以使用`setInterval`定时器来不断调用`drawSnows`方法,实现雪花飘落的动画效果。 ``` setInterval(() => { this.drawSnows() }, 30) ``` 完整代码如下: ``` <template> <canvas id="snow-canvas" canvas-id="snow-canvas" :style="{width: canvasWidth + 'px', height: canvasHeight + 'px'}"></canvas> </template> <script> class Snow { constructor(canvasWidth, canvasHeight) { this.x = Math.random() * canvasWidth this.y = Math.random() * canvasHeight this.radius = Math.random() * 3 + 1 this.speed = Math.random() * 2 + 1 this.alpha = Math.random() * (1 - 0.5) + 0.5 } draw(ctx) { ctx.beginPath() ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false) ctx.fillStyle = `rgba(255, 255, 255, ${this.alpha})` ctx.fill() } update(canvasWidth, canvasHeight) { this.y += this.speed if (this.y > canvasHeight) { this.y = 0 this.x = Math.random() * canvasWidth } } } export default { data() { return { canvasWidth: 0, canvasHeight: 0, ctx: null, snows: [] } }, mounted() { uni.getSystemInfo({ success: (res) => { this.canvasWidth = res.windowWidth this.canvasHeight = res.windowHeight const context = uni.createCanvasContext('snow-canvas', this) this.ctx = context } }) for (let i = 0; i < 100; i++) { const snow = new Snow(this.canvasWidth, this.canvasHeight) this.snows.push(snow) } setInterval(() => { this.drawSnows() }, 30) }, methods: { drawSnows() { this.ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight) for (let i = 0; i < this.snows.length; i++) { const snow = this.snows[i] snow.draw(this.ctx) snow.update(this.canvasWidth, this.canvasHeight) } uni.drawCanvas({ canvasId: 'snow-canvas', actions: this.ctx.getActions() }, this) } } } ```
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值