javascript - 如何根据图像上各个点的坐标值在图像上画一个点

标签 javascript jquery css

我有一张图片,我有图片上各个点的坐标值。 现在,当在网页上呈现图像时,我必须显示一个大点或任何用户可以看到的东西,如谷歌地图上的标记。

示例 - 假设谷歌地图作为我的图像(但它不会是 map ,它将是房屋的天花板图像)和 map 上的标记作为点。 我必须在图像上显示标记之类的东西。

如何使用 jquery、javascript、css...等实现这一点

感谢任何帮助..谢谢 :)

最佳答案

我得到了答案..:)
查看the fiddle here

HTML

<p>Click on the map to place a marker</p>
<canvas id="Canvas" width="700" height="700"></canvas>

Javascript

var canvas = document.getElementById('Canvas');
var context = canvas.getContext("2d");

// Map sprite
var mapSprite = new Image();
mapSprite.src = "http://www.retrogameguide.com/images/screenshots/snes-legend-of-zelda-link-to-the-past-8.jpg";

var Marker = function () {
    this.Sprite = new Image();
    this.Sprite.src = "http://www.clker.com/cliparts/w/O/e/P/x/i/map-marker-hi.png"
    this.Width = 12;
    this.Height = 20;
    this.XPos = 0;
    this.YPos = 0;
}

var Markers = new Array();

var mouseClicked = function (mouse) {
    // Get corrent mouse coords
    var rect = canvas.getBoundingClientRect();
    var mouseXPos = (mouse.x - rect.left);
    var mouseYPos = (mouse.y - rect.top);

    console.log("Marker added");

    // Move the marker when placed to a better location
    var marker = new Marker();
    marker.XPos = mouseXPos - (marker.Width / 2);
    marker.YPos = mouseYPos - marker.Height;

    Markers.push(marker);
}

// Add mouse click event listener to canvas
canvas.addEventListener("mousedown", mouseClicked, false);

var firstLoad = function () {
    context.font = "15px Georgia";
    context.textAlign = "center";
}

firstLoad();

var main = function () {
    draw();
};

var draw = function () {
    // Clear Canvas
    context.fillStyle = "#000";
    context.fillRect(0, 0, canvas.width, canvas.height);

    // Draw map
    // Sprite, X location, Y location, Image width, Image height
    // You can leave the image height and width off, if you do it will draw the image at default size
    context.drawImage(mapSprite, 0, 0, 700, 700);

    // Draw markers
    for (var i = 0; i < Markers.length; i++) {
        var tempMarker = Markers[i];
        // Draw marker
        context.drawImage(tempMarker.Sprite, tempMarker.XPos, tempMarker.YPos, tempMarker.Width, tempMarker.Height);

        // Calculate postion text
        var markerText = "Postion (X:" + tempMarker.XPos + ", Y:" + tempMarker.YPos;

        // Draw a simple box so you can see the position
        var textMeasurements = context.measureText(markerText);
        context.fillStyle = "#666";
        context.globalAlpha = 0.7;
        context.fillRect(tempMarker.XPos - (textMeasurements.width / 2), tempMarker.YPos - 15, textMeasurements.width, 20);
        context.globalAlpha = 1;

        // Draw position above
        context.fillStyle = "#000";
        context.fillText(markerText, tempMarker.XPos, tempMarker.YPos);
    }
};

setInterval(main, (1000 / 60)); // Refresh 60 times a second

关于javascript - 如何根据图像上各个点的坐标值在图像上画一个点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39253980/

相关文章:

javascript - jQuery:瞄准类(class)前 3 名

jquery - 移动设备上的 Bootstrap 响应问题

html - 左右两个标签的进度条

css - 响应式CSS,元素相互流入

javascript - 如何旋转以中心为中心的文本旋转到数字

javascript - 如何轻松地为记录的 JavaScript 函数创建 Github 友好的 Markdown ?

javascript - Node.js:在调用的脚本中访问数据库

javascript - 仅在浏览器(IE)中禁用图像缓存

javascript - 不显眼地调用带有参数的函数

javascript - 在变量中写入图像源并在 jquery 中更改 css 时使用该变量