gpt4 book ai didi

javascript - 使用 Canvas 将图像添加到轮子上

转载 作者:行者123 更新时间:2023-12-03 02:57:32 25 4
gpt4 key购买 nike

我是 Canvas 新手,我尝试创建一个轮子,这是我的代码:

drawWheel = () =>{
const length = 4;
const sliceDeg = 360/length;
var deg = 270;

var canvas = this.refs.canvas;
if(canvas.getContext){
const ctx = canvas.getContext('2d');
for(let i = 0;i<length;i++){
ctx.beginPath();
ctx.moveTo(center, center);
ctx.arc(center,center,radius, this.deg2rad(deg) ,this.deg2rad(deg + sliceDeg));
ctx.lineTo(center,center);
ctx.stroke();

ctx.save();
ctx.translate(center, center);
ctx.rotate(this.deg2rad(deg+sliceDeg/2));
ctx.textAlign = "right";
ctx.fillStyle = "red";
ctx.font = 'bold 5vw sans-serif';
ctx.fillText(i+1, radius/1.5,radius/11);
ctx.restore();
deg += sliceDeg;
}
}
}

这是我的结果:

enter image description here

如何填充每个切片的图像并调整它们以适合

最佳答案

看看globalCompositeOperation以及下面的示例:

var size = 400; // I use a simple size because i don't have any images. Your code should probably be a little smarter.
var myImages = []; //List of images
/* GENEREATE IMAGES START */
//I generate some images using canvas here, but you should probably use <IMG> elements and img.naturalWidth/img.naturalHeight
["red", "blue", "green", "yellow"].forEach((color) => {
var canvas = document.createElement("canvas");
canvas.width = canvas.height = size / 2;
var ctx = canvas.getContext("2d");
ctx.fillStyle = color;
ctx.fillRect(0, 0, size / 2, size / 2);
myImages.push(canvas);
});
/* GENEREATE IMAGES STOP */
//Main canvas to draw upon
var canvas = document.body.appendChild(document.createElement("canvas"));
canvas.width = canvas.height = size;
var ctx = canvas.getContext("2d");
//Draw base shape.
//Color doesn't matter, since we'll draw on top of it later
ctx.arc(size / 2, size / 2, size / 2, 0, Math.PI * 2);
ctx.fill();
//Set canvas to reuse transparency of existing pixels when drawing.
ctx.globalCompositeOperation = "source-atop";
//Loop through images and draw on canvas.
for (let x = 0; x < 2; x++) {
for (let y = 0; y < 2; y++) {
ctx.drawImage(myImages[x * 2 + y], x * size / 2, y * size / 2);
}
}
//Reset canvas draw mode.
//Technically not necessary, but a good practice to leave things the way you found them.
ctx.globalCompositeOperation = "source-over";

编辑 1

或者您可以使用 clipping获取图像中适合的部分:

编辑2

以下是使用剪切和处理 Angular 示例:

function drawImagesOnCircleSlices(p) {
var canvas = document.createElement("canvas");
canvas.width = canvas.height = p.radius * 2;
var ctx = canvas.getContext("2d");
var step = (Math.PI * 2) / p.images.length;
var cos = Math.cos(step);
var width = Math.ceil(Math.abs((Math.cos(0) * p.radius) - (cos * p.radius)));
var sin = Math.sin(step);
ctx.save();
ctx.translate(p.radius, p.radius);
for (var i = 0; i < p.images.length; i++) {
var image = p.images[i];
ctx.rotate(step);
ctx.save();
ctx.beginPath();
//Base
ctx.arc(0, 0, 0, 0, 0);
//Top
ctx.arc(0, 0, p.radius, 0, 0);
//Bottom
ctx.arc(0, 0, p.radius, 0, step);
ctx.closePath();
ctx.stroke();
ctx.clip();
//document.body.appendChild(image)
ctx.drawImage(image, Math.min(cos * p.radius, 0), Math.min(sin * p.radius, 0));
ctx.restore();
}
ctx.restore();
return canvas;
}
//TEST
//Asynchronous image loading with callback to draw
function loadImages(callback) {
var images = [];
var imageSize = 200;
var extra = 200;
var loaded = 0;
while (images.length < 7) {
var canvas = document.createElement("canvas");
canvas.height = Math.floor(Math.random() * imageSize) + extra;
canvas.width = Math.floor(Math.random() * imageSize) + extra;
var ctx = canvas.getContext("2d");
for (var i = 0; i < 1000; i++) {
ctx.fillStyle = "rgba(" + [Math.floor(Math.random() * 255), Math.floor(Math.random() * 255), Math.floor(Math.random() * 255), 0.5].join(",") + ")";
ctx.fillRect(Math.random() * imageSize, Math.random() * imageSize, Math.random() * imageSize, Math.random() * imageSize);
}
var img = document.createElement("img");
images.push(img);
img.onload = function() {
loaded++;
if (loaded === images.length) {
callback(images);
}
};
img.src = canvas.toDataURL();
}
}
//Begin loading
loadImages(function(images) {
document.body.appendChild(drawImagesOnCircleSlices({
images: images,
radius: 200
}));
});

关于javascript - 使用 Canvas 将图像添加到轮子上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47550921/

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