最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to draw a point on image based on coordinate value of various points on image - Stack Overflow

programmeradmin2浏览0评论

I have an image, i have coordinate value of various points on image. Now when image is rendered on web page, I have to display a big point or anything which user can see as a point like markers on google map.

example - Assume google map as my image(but it wont be a map, it will be ceiling image of a home) and markers on map as points. I have to display marker kind of thing on my image.

How this can be achieved using jquery, javascript, css...etc.

Any help appreciated..Thanks :)

I have an image, i have coordinate value of various points on image. Now when image is rendered on web page, I have to display a big point or anything which user can see as a point like markers on google map.

example - Assume google map as my image(but it wont be a map, it will be ceiling image of a home) and markers on map as points. I have to display marker kind of thing on my image.

How this can be achieved using jquery, javascript, css...etc.

Any help appreciated..Thanks :)

Share Improve this question edited Aug 31, 2016 at 16:02 satyendra kumar asked Aug 31, 2016 at 15:48 satyendra kumarsatyendra kumar 7692 gold badges10 silver badges19 bronze badges 5
  • Position the element absolutely within the container, then use top/bottom and left/right to place it at the required co-ordinates – Rory McCrossan Commented Aug 31, 2016 at 15:49
  • Sorry, but I could not understand anything from your answer @ Rory – satyendra kumar Commented Aug 31, 2016 at 15:51
  • 1 This might help - codepen.io/Paulie-D/pen/dgcha – Paulie_D Commented Aug 31, 2016 at 15:54
  • This is what my requirement is but bit different, I have a set of coordinate points and at all these points I have to put markers. How that can be achieved? By the way thanks Paulie :) – satyendra kumar Commented Aug 31, 2016 at 15:58
  • Well if they are coordinates they must have a reference system...surely that can be adapted. – Paulie_D Commented Aug 31, 2016 at 16:08
Add a ment  | 

1 Answer 1

Reset to default 6

I got the answer.. :)
check out 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./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./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
发布评论

评论列表(0)

  1. 暂无评论