我正在用html5畫布創造財富圈的輪子。它與填充樣式顏色工作正常。我想要隨機切片中的兩(2)個不同圖像填充樣式圖案。我如何實現這一目標。如何用不同的圖像填充cannvas圓弧?
這裏是我JS
function rand(min, max) {
return Math.random() * (max - min) + min;
}
var color = ['#fbc','#f88','#fbc','#f88','#fbc','#f88', "#fbc", "#f67"];
var label = ['10', '200', '50', '100', '5', '500', '0', "jPOT"];
var slices = color.length;
var sliceDeg = 360/slices;
var deg = rand(0, 360);
var speed = 0;
var slowDownRand = 0;
var ctx = canvas.getContext('2d');
var width = canvas.width; // size
var center = width/2; // center
var isStopped = false;
var lock = false;
function deg2rad(deg) {
return deg * Math.PI/180;
}
function drawSlice(deg, color) {
ctx.beginPath();
ctx.fillStyle = color;
ctx.moveTo(center, center);
ctx.arc(center, center, width/2, deg2rad(deg), deg2rad(deg+sliceDeg));
ctx.lineTo(center, center);
ctx.fill();
}
function drawText(deg, text) {
ctx.save();
ctx.translate(center, center);
ctx.rotate(deg2rad(deg));
ctx.textAlign = "right";
ctx.fillStyle = "#fff";
ctx.font = 'bold 30px sans-serif';
ctx.fillText(text, 130, 10);
ctx.restore();
}
function drawImg() {
ctx.clearRect(0, 0, width, width);
for(var i=0; i<slices; i++){
drawSlice(deg, color[i]);
drawText(deg+sliceDeg/2, label[i]);
deg += sliceDeg;
}
}
document.getElementById("spin").addEventListener("mousedown", function(){
isStopped = true;
}, false);
drawImg();
document.getElementById("play").addEventListener("mousedown", function(){
(function anim() {
deg += speed;
deg %= 360;
// Increment speed
if(!isStopped && speed<3){
speed = speed+1 * 8;
}
// Decrement Speed
if(isStopped){
if(!lock){
lock = true;
slowDownRand = rand(0.994, 0.998);
}
speed = speed>0.2 ? speed*=slowDownRand : 0;
}
// Stopped!
if(lock && !speed){
var ai = Math.floor(((360 - deg - 90) % 360)/sliceDeg); // deg 2 Array Index
ai = (slices+ai)%slices; // Fix negative index
return alert("You got:\n"+ label[ai]); // Get Array Item from end Degree
}
drawImg();
window.requestAnimationFrame(anim);
}());
}, false);
這裏是我的html
<div id="wheel">
<canvas id="canvas" width="300" height="300"></canvas>
</div>
<button id="spin">Stop!</button>
<button id="play">play!</button>
請幫助我。 Working fiddle is here
你能提供什麼你到底想達到一個靜態的樣機? – Kaiido
其實在我的畫布圈裏他們是8個部分。我想隨機設置兩個不同的@Kaiido圖像作爲模式。 [請找到圖像的附加鏈接](https://ibb.co/cam8vk)我想要什麼。我實現了文字和圓圈,但無法實現模式。你可以查看[我的小提琴也](https://jsfiddle.net/as88425/12j4fvmL/) – Abhishek