Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在可缩放和可平移的图像上添加标记_Javascript_Html_Css_Reactjs - Fatal编程技术网

Javascript 在可缩放和可平移的图像上添加标记

Javascript 在可缩放和可平移的图像上添加标记,javascript,html,css,reactjs,Javascript,Html,Css,Reactjs,我目前正在我的网站上使用react zoom pan pinch作为图像查看器。我想在图像的特定坐标处添加图标,但我不确定如何添加 以下是图像查看器外观的示例: 假设我想在道路上放置一个红色标记,当我移动图像时,它将保持在相同的坐标/像素。即使API不特别支持,也可以这样做吗?我是否可以使用其他API来简化这项工作?我还希望能够应用一些css的标记 我发现了一个类似的问题,它已经解决了 Html 单击地图以放置标记 Javascript // Target the canvas element

我目前正在我的网站上使用react zoom pan pinch作为图像查看器。我想在图像的特定坐标处添加图标,但我不确定如何添加

以下是图像查看器外观的示例:

假设我想在道路上放置一个红色标记,当我移动图像时,它将保持在相同的坐标/像素。即使API不特别支持,也可以这样做吗?我是否可以使用其他API来简化这项工作?我还希望能够应用一些css的标记

我发现了一个类似的问题,它已经解决了

Html

单击地图以放置标记

Javascript

// Target the canvas element on the page
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";

// Create a basic class which will be used to create a marker
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;
}

// Array of markers
var Markers = new Array();

// When the user clicks their mouse on our canvas run this code
var mouseClicked = function (mouse) {
    // Get corrent mouse coords
    var rect = canvas.getBoundingClientRect();
    var mouseXPos = (mouse.x - rect.left);
    var mouseYPos = (mouse.y - rect.top);

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

    // Push our new marker to our Markers array
    Markers.push(marker);
}

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

// Run this once so we setup text rendering
var firstLoad = function () {
    context.font = "15px Georgia";
    context.textAlign = "center";
}

firstLoad();

// This will be called 60 times a second, look at the code at the bottom `setInterval`
var main = function () {
    // Update our canvas
    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 position 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
//以页面上的canvas元素为目标
var canvas=document.getElementById('canvas');
var context=canvas.getContext(“2d”);
//地图精灵
var mapSprite=新图像();
mapSprite.src=”http://www.retrogameguide.com/images/screenshots/snes-legend-of-zelda-link-to-the-past-8.jpg";
//创建用于创建标记的基本类
变量标记=函数(){
this.Sprite=新图像();
this.Sprite.src=”http://www.clker.com/cliparts/w/O/e/P/x/i/map-marker-hi.png"
这个。宽度=12;
这个。高度=20;
这个.XPos=0;
这1.YPos=0;
}
//标记数组
var Markers=新数组();
//当用户在画布上单击鼠标时,运行此代码
var mouseClicked=函数(鼠标){
//获得正确的鼠标坐标
var rect=canvas.getBoundingClientRect();
var mouseXPos=(mouse.x-rect.left);
var mouseYPos=(mouse.y-rect.top);
//将标记放置到更好的位置时移动标记
var marker=新标记();
marker.XPos=mouseXPos-(marker.Width/2);
marker.YPos=mouseYPos-marker.Height;
//将新标记器推送到标记器阵列
标记器。推(标记器);
}
//将鼠标单击事件侦听器添加到画布
canvas.addEventListener(“mousedown”,mouseClicked,false);
//运行此命令一次,以便设置文本渲染
var firstLoad=函数(){
context.font=“15px格鲁吉亚”;
context.textAlign=“中心”;
}
firstLoad();
//这将每秒调用60次,请查看底部的代码`setInterval'`
var main=函数(){
//更新我们的画布
draw();
};
var draw=函数(){
//透明帆布
context.fillStyle=“#000”;
context.fillRect(0,0,canvas.width,canvas.height);
//绘制地图
//精灵,X位置,Y位置,图像宽度,图像高度
//您可以关闭图像的高度和宽度,如果这样做,将以默认大小绘制图像
drawImage(mapSprite,0,0700700);
//画记号笔
对于(var i=0;i

但是,我不会单击添加标记,而是通过从数据库检索到的坐标添加标记。我还希望图像可以像上面显示的链接一样平移和缩放。

找到了解决方案吗?
// Target the canvas element on the page
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";

// Create a basic class which will be used to create a marker
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;
}

// Array of markers
var Markers = new Array();

// When the user clicks their mouse on our canvas run this code
var mouseClicked = function (mouse) {
    // Get corrent mouse coords
    var rect = canvas.getBoundingClientRect();
    var mouseXPos = (mouse.x - rect.left);
    var mouseYPos = (mouse.y - rect.top);

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

    // Push our new marker to our Markers array
    Markers.push(marker);
}

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

// Run this once so we setup text rendering
var firstLoad = function () {
    context.font = "15px Georgia";
    context.textAlign = "center";
}

firstLoad();

// This will be called 60 times a second, look at the code at the bottom `setInterval`
var main = function () {
    // Update our canvas
    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 position 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