gpt4 book ai didi

javascript - 改变图像的形状

转载 作者:行者123 更新时间:2023-11-29 17:34:45 25 4
gpt4 key购买 nike

我需要有关在 html Canvas 中将移动方形图像转换为圆形的帮助

我导入了一个正方形的图像。我希望该图像是圆的并且仍然在移动。

目标:

  • 方形图像到圆形图像,并且仍然可以移动。

我尝试了很多关于 stackoverflow 的教程,主要是用 c 语言。 stroke 和 c.split 但是当我应用它们时图像不再移动。

有人有什么建议吗?

var canvas = document.querySelector('canvas');
var c = canvas.getContext('2d');

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

function Circle() {
//Give var for circle
this.x = 10;
this.y = 100;
this.dx = 1;
this.dy = 1;
this.radius = 50;
this.diameter = 2 * this.radius;

//Get external square picture (Needs to be converted in circle)
var image = new Image();
image.src = "https://assets.coingecko.com/coins/images/2607/large/molecular_future.png?1547036754";

//Draw circle on canvas
this.draw = function () {
//Circle
c.beginPath();
c.arc(this.x, this.y, (this.radius*1), 0, Math.PI * 2, false);
c.closePath();

//TODO: cut square to circle

//Place square (image) on top of the circle
c.drawImage(image, (this.x-this.diameter/2) , (this.y-this.diameter/2), this.diameter, this.diameter);
};

//Update position
this.update = function () {
this.x += this.dx;
this.draw()
}
}

//Animate canvas
function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, innerWidth, innerHeight);
this.update();
}

//Start
Circle();
animate();
canvas {
border: 1px solid black;
background: black;

}
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<canvas></canvas>

</body>
</html>

最佳答案

答案是在你的情况下使用 context.clip(); c.clip(); 这会在 Canvas 上创建一个剪辑过滤器,然后你可以绘制在制作剪辑之前,您必须先保存,然后在分别使用 c.save();c.restore() 绘制后恢复。

var canvas = document.querySelector('canvas');
var c = canvas.getContext('2d');
var circles = [];

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

function Circle() {
//Give var for circle
this.x = 100;
this.y = 100;
this.dx = 1;
this.dy = 1;
this.radius = 50;
this.diameter = 2 * this.radius;
this.size = null;
this.c = null;

//Get external square picture (Needs to be converted in circle)
var image = new Image();
image.src = "https://assets.coingecko.com/coins/images/2607/large/molecular_future.png?1547036754";

//Draw circle on canvas
this.draw = function () {
//Circle
this.c.beginPath();
this.c.arc(this.x, this.y, (this.radius*1), 0, Math.PI * 2, false);
this.c.closePath();
this.c.save();
this.c.clip();
//TODO: cut square to circle

//Place square (image) on top of the circle
this.c.drawImage(image, (this.x-this.diameter/2) , (this.y-this.diameter/2), this.diameter, this.diameter);
this.c.restore();
};

//Update position
this.update = function () {
if(this.x - this.radius <= 0 || this.x + this.radius >= this.size.x){this.dx = -this.dx}

this.x += this.dx;
this.draw()
}

this.init = function(options) {

Object.keys(options).forEach((key)=>{
this[key]=options[key];
})
}
}

//Animate canvas
function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, innerWidth, innerHeight);
for(let i = 0; i < circles.length; i++){ circles[i].update(); }
}

//Start
for(let i = 0; i < 100; i-=-1){
let circle = new Circle();
circle.init({
x: Math.random() * window.innerWidth,
y: Math.random() * window.innerHeight,
size: {x: window.innerWidth,y: window.innerHeight},
c
})
circles.push(circle)
}
animate();
canvas {
border: 1px solid black;
background: black;

}
body {
margin: 0;
padding: 0;
overflow-x: hidden;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<canvas></canvas>

</body>
</html>

关于javascript - 改变图像的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57924261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com