html5 canvas 文档,HTML5 Canvas保存为本地图片文件

本文介绍了一个使用JavaScript实现的气泡动画效果,通过设置不同的参数来调整气泡的大小、速度等属性,使得动画更加生动有趣。同时,文章还提供了一个导出当前画布为图片的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

var minRadius = 30;

var bubbleNum = 1;

var depth = 100;

var canvas = document.getElementById('canvastoimage');

var ctx = canvas.getContext('2d');

canvas.width = innerWidth;

canvas.height = innerHeight;

window.addEventListener('resize', function() {

canvas.width = innerWidth;

canvas.height = innerHeight;

generateBubbles();

})

function Bubble() {

this.gravity = Math.random() * 0.02;

this.radius = minRadius + Math.random() * 40;

this.x = this.radius + (Math.random() * (innerWidth - this.radius * 2));

this.y = this.radius + (Math.random() * (innerHeight - this.radius * 2));

this.z = Math.random() * depth;

this.hspeed = 0.5 + (Math.random() * -1);

this.vspeed = 0.5 + (Math.random() * -1);

}

Bubble.prototype.move = function() {

if (this.x + this.hspeed < this.radius || this.x + this.hspeed > innerWidth - this.radius) {

this.hspeed *= -1;

}

if (this.y + this.vspeed < this.radius || this.y + this.vspeed > innerHeight - this.radius) {

this.vspeed *= -1;

}

this.x += this.hspeed;

this.y += this.vspeed;

this.vspeed += this.gravity;

if (this.x > innerWidth || this.x < 0 || this.y > innerHeight || this.y < 0) {

this.x = this.radius + (Math.random() * (innerWidth - this.radius * 2));

this.y = this.radius + (Math.random() * (innerHeight - this.radius * 2));

}

};

Bubble.prototype.draw = function() {

var bubble = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.radius);

bubble.addColorStop(0, 'rgba(98, 79, 249, 0.05)');

bubble.addColorStop(1, 'rgba(98, 79, 249, 0.3)');

ctx.fillStyle = bubble;

ctx.beginPath();

ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);

ctx.fill();

var glare = ctx.createRadialGradient(this.x, this.y, this.radius * 0.2, this.x, this.y, this.radius * 1.1);

glare.addColorStop(0, 'rgba(255,255,255,0)');

glare.addColorStop(1, 'rgba(255,255,255,0.6)');

ctx.strokeStyle = glare;

ctx.lineWidth = this.radius * 0.25;

ctx.beginPath();

ctx.arc(this.x, this.y, this.radius * 0.66, Math.PI * -0.05, Math.PI * 1.55, true);

ctx.stroke();

ctx.lineWidth = this.radius * 0.1;

ctx.beginPath();

ctx.arc(this.x, this.y, this.radius * 0.7, Math.PI * 0.65, Math.PI * 0.9, false);

ctx.stroke();

};

var bubbles = [];

function generateBubbles() {

for (var i = bubbles.length; i < bubbleNum; i++) {

bubbles.push(new Bubble());

}

}

generateBubbles();

function loop() {

ctx.clearRect(0, 0, innerWidth, innerHeight);

for (var i = 0; i < bubbles.length; i++) {

bubbles[i].draw();

bubbles[i].move();

}

requestAnimationFrame(loop);

}

requestAnimationFrame(loop);

function saveAsImage() {

var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"); // here is the most important part because if you dont replace you will get a DOM 18 exception.

//save as download without name and extension

//window.location.href = image;

var link = document.getElementById('link');

link.setAttribute('download', 'my.png');

link.setAttribute('href', canvas.toDataURL("image/png").replace("image/png", "image/octet-stream"));

link.click();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值