【问题标题】:How to show tooltip on the dynamic images using KineticJS如何使用 KineticJS 在动态图像上显示工具提示
【发布时间】:2013-11-27 17:14:27
【问题描述】:

我想在画布内的动态图像上显示工具提示。这是我要在其上显示工具提示的图像的路径。(我想在绿色和灰色图像上显示工具提示)。 enter link description here

这是我在 kinetic.js 的帮助下使用的代码。

 $("#tabs li").each(function () {
            $(this).live("click", function () {
                var tabname = $(this).find("a").attr('name');
                tabname = $.trim(tabname.replace("#tab", ""));
                var tabId = $(this).find("a").attr('href');
                tabId = $.trim(tabId.replace("#", ""));

                $.ajax({
                    url: "/Home/GetTabsDetail",
                    dataType: 'json',
                    type: 'GET',
                    data: { tabId: tabId },
                    cache: false,
                    success: function (data) {
                        var bayStatus = [];
                        var i = 0;
                        var image_array = [];
                        var BayExist = false;
                        var BayCondition;
                        var imgSrc;
                        var CanvasBacgroundImage;
                        var _X;
                        var _bayNumber;
                        var _Y;
                        var _ZoneName;
                        $(data).each(function (i, val) {
                            i = i + 1;
                            if (!BayExist) {
                                bayStatus = val.BayStatus;
                                CanvasBacgroundImage = val.TabImageLocation;
                                BayExist = true;
                            }
                            $.each(val, function (k, v) {
                                if (k == "BayNumber") {
                                    BayCondition = bayStatus[v];
                                    _bayNumber = v;
                                    if (BayCondition == "O")
                                        imgSrc = "../../images/Parking/OccupiedCar.gif"
                                    else if (BayCondition == "N")
                                        imgSrc = "../../images/Parking/OpenCar.gif";
                                }
                                if (k == "BayX")
                                    _X = v;
                                if (k == "BayY")
                                    _Y = v;
                                if (k == "ZoneName")
                                    _ZoneName = v;
                            });
                            image_array.push({ img: imgSrc, xAxis: _X, yAxis: _Y, toolTip: _bayNumber, ZoneName: _ZoneName });
                        });
                        var imageUrl = CanvasBacgroundImage;

                        $('#tab' + tabId).css('background-image', 'url("../../images/Parking/' + imageUrl + '")');
                        var _timer = setInterval(function () {
                            var stage = new Kinetic.Stage({
                                container: 'tab' + tabId,
                                width: 700,
                                height: 308
                            });
                            var layer = new Kinetic.Layer();
                            var planetOverlay;
                            function writeMessage(message) {
                                text.setText(message);
                                layer.draw();
                            }
                            var text = new Kinetic.Text({
                                x: 140,
                                y: 0,
                                fontFamily: 'Arial',
                                fontSize: 20,

                                text: '',
                                fill: '#000',
                                width: 740,
                                height: 60,
                                align: 'left',
                                padding: 40,


                            });
                            for (i = 0; i < image_array.length; i++) {
                                img = new Image();
                                debugger;
                                img.src = image_array[i].img;
                                planetOverlay = new Kinetic.Image({
                                    x: image_array[i].xAxis,
                                    y: image_array[i].yAxis,
                                    image: img,
                                    height: 18,
                                    width: 18,
                                    id: image_array[i].toolTip,
                                    name: image_array[i].ZoneName
                                });

                                planetOverlay.on('mouseover', function () {
                                    writeMessage("Bay: " + this.getId() + " , Zone: " + this.getName());
                                });
                                planetOverlay.on('mouseout', function () {
                                    writeMessage('');
                                });
                                planetOverlay.createImageHitRegion(function () {
                                    layer.draw();
                                });

                                layer.add(planetOverlay);
                                layer.add(text);
                                stage.add(layer);
                            }

                            clearInterval(_timer);
                            //$("#tab3 .kineticjs-content").find("canvas").css('background-image', 'url("' + imageUrl + '")');
                        }, 1000)


                    },
                    error: function (result) {
                        alert('error');
                    }
                });
            });
        });

这是 html 部分。

<div style="background-position: center center; margin: 0px auto; padding-top: 50px; background-repeat: no-repeat; width: 700px; height: 308px; display: block; background-image: url(&quot;../../images/Parking/Garage-Floor-Plan.png&quot;);" id="tab3"><div style="position: relative; display: inline-block; width: 700px; height: 308px;" class="kineticjs-content" role="presentation"><canvas style="padding: 0px; margin: 0px; border: 0px none; background: none repeat scroll 0% 0% transparent; position: absolute; top: 0px; left: 0px; width: 700px; height: 308px;" width="700" height="308"></canvas></div></div>

上面提到的代码现在可以在每个图像鼠标悬停但在提到的坐标上显示单个消息,这里我想在每个图像的鼠标悬停时显示唯一消息(图中的绿色/灰色)如给定的图片和我要显示工具提示的图像上方。

此外,需要双击才能将图像绑定到选项卡的画布。

提前感谢您的帮助。

【问题讨论】:

  • 我在遵循您的代码逻辑时遇到了问题。您的 setInterval 重复(每 200 毫秒)加载同一组图像并从这些图像创建 kinetic.images。为什么要重复……在 200 毫秒内问太多了。
  • 根据需要更改代码以使其工作。根据您的说法,您的意思是 Kinetic 也在生成图像?我需要在给定图片中的图像上实现工具提示。欢迎提出任何建议。
  • 代码已修改。

标签: kineticjs


【解决方案1】:

而不是那个疯狂的令人困惑的脚本。为什么不直接使用纯 JavaScript? 最好以正确的顺序绘制画布。 这是我认为您正在寻找的简单演示。 随意编辑代码并添加功能: 现场演示: Codepen Demo

单击编辑按钮并查看代码。

或者在这里查看代码:

var canvas = document.getElementById('myCanvas');
canvas.width = 500;
canvas.width = 500;
var ctx = canvas.getContext('2d');



var image1 = new Image();
image1.onload = function() {
        // Image is Loaded, let's start the render() function!
        render();
};
image1.src = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg';

// Global X and Y from the MouseMove - to check if hovering over Vader
var x = 0, y = 0;
// This is a Simple Timeout. Since you want to re-draw the image often.
function render(){
  // Lets pain the Canvas Grey!
  ctx.fillStyle='grey';
  ctx.fillRect(0,0,canvas.width,canvas.height);

  // Draw Vader
  ctx.drawImage(image1, 200, 20, 200, 200);

  // These are roughly the positions of VADER, now the text will only display when you move over VADER!
  if(x > 275 && x < 540 && y > 35 && y < 210){
    ctx.fillStyle= 'black';
    ctx.fillRect(200,20,80,40);

    ctx.fillStyle= 'white';
    ctx.font="20px Georgia";
    ctx.fillText("VADER",205,45);
  }

  // Repeat every 200ms
  setTimeout(function(){
     render();
  },200);
}

window.addEventListener( 'mousemove', check_mouse_position);

// When the mouse is moved, it will call the function below:
function check_mouse_position(e){
  x = e.x;
  y = e.y;
}

这应该简单得多。使用鼠标的 X 和 Y 位置找出显示文本的位置...

【讨论】:

  • 您好 Schoening 感谢您的帮助。代码现已修改,请您看一次。现在这段代码工作正常,只需单击两次即可将图像绑定到舞台层,而且我现在想在图像上显示工具提示,而不是在某个固定的 x,y 上显示它现在工作。
  • 您所要做的就是将 X 和 Y 设置到正确的位置。只需在更新或其他任何东西上更改它们即可。比如:为 X 和 Y 创建两个全局变量,并将它们的值更改为更新时的正确位置
  • 好的。谢谢 。以及如何使它在单击 li 选项卡时工作,目前它正在单击选项卡上的两次单击我已经给出了时间间隔,但仍然图像没有得到绑定到舞台层一次单击。你介意帮我做这件事吗?
【解决方案2】:

修改后的代码在下面,现在可以按要求工作。我在 kinecticjs 和 Opentip 的帮助下设法做到了。这是输出图片here

$("#tabs li").each(function () {
            $(this).live("click", function () {
                var tabname = $(this).find("a").attr('name');
                tabname = $.trim(tabname.replace("#tab", ""));
                var tabId = $(this).find("a").attr('href');
                tabId = $.trim(tabId.replace("#", ""));
                $.ajax({
                    url: "/Home/GetTabsDetail",
                    dataType: 'json',
                    type: 'GET',
                    data: { tabId: tabId },
                    cache: false,
                    success: function (data) {
                        var bayStatus = [];
                        var i = 0;
                        var image_array = [];
                        var BayExist = false;
                        var BayCondition;
                        var imgSrc;
                        var CanvasBacgroundImage;
                        var _X;
                        var _bayNumber;
                        var _Y;
                        var _ZoneName;
                        $(data).each(function (i, val) {
                            i = i + 1;
                            if (!BayExist) {
                                bayStatus = val.BayStatus;
                                CanvasBacgroundImage = val.TabImageLocation;
                                BayExist = true;
                            }
                            $.each(val, function (k, v) {
                                if (k == "BayNumber") {
                                    BayCondition = bayStatus[v];
                                    _bayNumber = v;
                                    if (BayCondition == "O")
                                        imgSrc = "../../images/Parking/OccupiedCar.gif"
                                    else if (BayCondition == "N")
                                        imgSrc = "../../images/Parking/OpenCar.gif";
                                }
                                if (k == "BayX")
                                    _X = v;
                                if (k == "BayY")
                                    _Y = v;
                                if (k == "ZoneName")
                                    _ZoneName = v;
                            });
                            image_array.push({ img: imgSrc, xAxis: _X, yAxis: _Y, toolTip: _bayNumber, ZoneName: _ZoneName });
                        });
                        var imageUrl = CanvasBacgroundImage;
                        $('#tab' + tabId).css('background-image', 'url("../../images/Parking/' + imageUrl + '")');
                        var _timer = setInterval(function () {
                            var stage = new Kinetic.Stage({
                                container: 'tab' + tabId,
                                width: 700,
                                height: 308
                            });
                            var layer = new Kinetic.Layer();
                            var planetOverlay;
                            function writeMessage(message, _x, _y) {
                                text.setX(_x + 20);
                                text.setY(_y + 1);
                                text.setText(message);
                                layer.draw();
                            }
                            var text = new Kinetic.Text({
                                fontFamily: 'Arial',
                                fontSize: 14,
                                text: '',
                                fill: '#000',
                                width: 200,
                                height: 60,
                                align: 'center'

                            });
                            var opentooltip = new Opentip(
                                "div#tab" + tabId, //target element 
                                "dummy", // will be replaced
                                "", // title
                                {
                                showOn: null // I'll manually manage the showOn effect
                            });
                            Opentip.styles.win = {
                                borderColor: "black",
                                shadow: false,
                                background: "#EAEAEA"
                            };
                            Opentip.defaultStyle = "win";
                            for (i = 0; i < image_array.length; i++) {
                                img = new Image();
                                debugger;
                                img.src = image_array[i].img;
                                planetOverlay = new Kinetic.Image({
                                    x: image_array[i].xAxis,
                                    y: image_array[i].yAxis,
                                    image: img,
                                    height: 18,
                                    width: 18,
                                    id: image_array[i].toolTip,
                                    name: image_array[i].ZoneName
                                });
                                planetOverlay.on('mouseover', function () {
                                    opentooltip.setContent("<span style='color:#25A0D3;'>Bay:</span> " + this.getId() + "<br> <span style='color:#25A0D3;'>Zone:</span><span>" + this.getName() + "</span>");
                                    //writeMessage("Bay: " + this.getId() + " , Zone: " + this.getName(), this.getX(), this.getY());//other way of showing tooltip
                                    opentooltip.show();
                                    //$("#opentip-1").offset({ left: this.getX(), top: this.getY() });
                                });
                                planetOverlay.on('mouseout', function () {
                                    opentooltip.hide();
                                    // writeMessage('');
                                });
                                planetOverlay.createImageHitRegion(function () {
                                    layer.draw();
                                });
                                layer.add(planetOverlay);
                                layer.add(text);
                                stage.add(layer);
                            }
                            clearInterval(_timer);
                            //$("#tab3 .kineticjs-content").find("canvas").css('background-image', 'url("' + imageUrl + '")');
                        }, 1000)
                    },
                    error: function (result) {
                        alert('error');
                    }
                });
            });
        }); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多