在 html5 canvas 标签中创建图像的分层树结构?

发布于 2024-12-23 01:47:53 字数 1651 浏览 1 评论 0原文

一组图像之间存在树状层次关系,我希望图像在画布标记中表示为树结构,其中子节点图像小于父节点图像,并且所有图像应在鼠标悬停时按一定值放大。

setscale 和translate javascript 函数让我可以很好地实现缩放效果,但我无法使鼠标事件侦听器函数正常工作,尽管显示了图像,但未检测到鼠标悬停和鼠标移出。我没有收到用于调试的警报消息。这只是一个测试代码:-

     function loadImages(sources, callback){
       var images = {};
       var loadedImages = 0;
       var numImages = 0;
       // get num of sources
       for (var src in sources) {
        numImages++;
       }
       for (var src in sources) {
        images[src] = new Image();
        images[src].onload = function(){
            if (++loadedImages >= numImages) {
                callback(images);
            }
        };
        images[src].src = sources[src];
       }
       }  
 
 
 
    window.onload = function(){
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
    var destX = 20;
    var destY = 20;
    var destWidth = 200;
    var destHeight = 137;
    
    var sources = new Array();
    sources[0]="darth-vader.jpg";
    sources[1]="darth-vader.jpg";
    
    var imageObj = new Image();
 
    loadImages(sources, function(images){
        context.drawImage(images[0], destX, destY, destWidth, destHeight);
         images[0].onmouseover=function()
        {
            alert('1');
        }

        images[0].onmouseout=function()
        {
            alert('2');
        }
        context.drawImage(images[1], destX+200,destY, destWidth, destHeight);
        images[1].onmouseover=function()
        {
            alert('3');
        }

        images[1].onmouseout=function()
        {
            alert('4');
        }
          });
         };

There is a tree hierarchical relation between a group of images, I want images to be represented as a tree structure in canvas tag with child node images smaller than parent node images and all the images should scale up with some value on mouseover.

The setscale and translate javascript functions let me do the scaling effect all right but I cannot get mouse event listener functions working right, mouseover and mouseout are not being detected although the images are displayed. I do not get the alert messages I have used for debugging. This is just a test code:-

     function loadImages(sources, callback){
       var images = {};
       var loadedImages = 0;
       var numImages = 0;
       // get num of sources
       for (var src in sources) {
        numImages++;
       }
       for (var src in sources) {
        images[src] = new Image();
        images[src].onload = function(){
            if (++loadedImages >= numImages) {
                callback(images);
            }
        };
        images[src].src = sources[src];
       }
       }  
 
 
 
    window.onload = function(){
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
    var destX = 20;
    var destY = 20;
    var destWidth = 200;
    var destHeight = 137;
    
    var sources = new Array();
    sources[0]="darth-vader.jpg";
    sources[1]="darth-vader.jpg";
    
    var imageObj = new Image();
 
    loadImages(sources, function(images){
        context.drawImage(images[0], destX, destY, destWidth, destHeight);
         images[0].onmouseover=function()
        {
            alert('1');
        }

        images[0].onmouseout=function()
        {
            alert('2');
        }
        context.drawImage(images[1], destX+200,destY, destWidth, destHeight);
        images[1].onmouseover=function()
        {
            alert('3');
        }

        images[1].onmouseout=function()
        {
            alert('4');
        }
          });
         };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぃ弥猫深巷。 2024-12-30 01:47:53

事件在 dom 元素上触发,而不是在画布上绘制的图像上触发。如果您想模拟这些事件,请尝试在画布上设置 mousemove 事件,然后如果鼠标位于您绘制图像的区域内,则会触发鼠标悬停。

Events are triggered on dom elements not on images painted on a canvas. If you want to simulate those events try to set a mousemove event on the canvas and then if the mouse is inside the area where you have drawn the image trigger the mouseover.

难理解 2024-12-30 01:47:53

您只能将鼠标事件放在画布本身上,而不能放在绘制到画布上的图像上。

您必须开始跟踪所绘制图像的大小和位置,以便在用户单击画布时可以执行相关操作。有关如何跟踪的示例,请参阅让 Canvas 变得有用的简要介绍画布上的多个可选对象。

You can only put mouse events on the canvas itself, not on the images drawn to it.

You're going to have to start keeping track of the size and location of images drawn so you can do relevant things when the user clicks on the canvas. See A Gentle Introduction to Making Canvas Useful for examples of how to keep track of multiple selectable objects on a canvas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文