所以,我一直在試圖在這個黃色圓圈上繪製圖像(標記)。我嘗試在畫布圓圈內的圖像上顯示隨機點/標記。我想顯示工具提示是這樣的: HTML5畫布圖像工具提示
這是我的代碼:
function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
//create circle
ctx.beginPath();
ctx.arc(190, 190, 190, 0, 2 * Math.PI);
//circle background color
ctx.fillStyle = 'rgba(255, 238, 48, 0.11)';
ctx.fill();
//circle border
ctx.lineWidth = 1;
ctx.strokeStyle = '#FFC500';
ctx.stroke();
ctx.closePath();
var xCor, yCor, p1, p2, pcolor, posY = 100,
txtwidth, txtLoc = "hello world";
function getXY() { //get x/y random coordinate
xCor = randomIntFromInterval(50, 300);
yCor = randomIntFromInterval(50, 300);
}
function drawTextBG(ctx, txtLoc, font, x, y) {
ctx.save();
ctx.font = font;
ctx.textBaseline = 'top';
ctx.fillStyle = '#fff';
var width = ctx.measureText(txtLoc).width;
ctx.fillRect(x, y, width, parseInt(font, 10));
ctx.fillStyle = '#000';
ctx.fillText(txtLoc, x, y);
ctx.restore();
}
function drwLine(p1, p2, pcolor) {
ctx.beginPath();
ctx.moveTo(p1 + 10, p2);
ctx.lineTo(100, 100);
ctx.strokeStyle = pcolor;
ctx.lineWidth = 2;
ctx.stroke();
ctx.closePath();
console.log(p1 + ' ' + p2);
}
//insert image map marker
var t, mark = new Image();
mark.src = "http://icons.iconarchive.com/icons/e-young/gur-project/32/map-pointer-icon.png";
mark.onload = function() {
getXY();
ctx.drawImage(mark, xCor, yCor);
drwLine(xCor, yCor, '#d93030');
//drawTextBG(ctx, txtLoc, '14px arial', 30, 30);
//console.log(xCor+' '+yCor);
getXY();
ctx.drawImage(mark, xCor, yCor);
drwLine(xCor, yCor, 'd93030');
//console.log(xCor+' '+yCor);
getXY();
ctx.drawImage(mark, xCor, yCor);
drwLine(xCor, yCor, 'd93030');
//console.log(xCor+' '+yCor);
getXY();
ctx.drawImage(mark, xCor, yCor);
drwLine(xCor, yCor, 'd93030');
//console.log(xCor+' '+yCor);
getXY();
ctx.drawImage(mark, xCor, yCor);
drwLine(xCor, yCor, 'd93030');
//console.log(xCor+' '+yCor);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<canvas id="myCanvas" class="mycanv" height="390" width="390"> Your browser does not support the HTML5 canvas tag.</canvas>
我認爲這是缺少代碼... – 2014-09-19 10:09:07
http://jsfiddle.net/oezzrrzr/ – user2633451 2014-09-19 10:14:28