首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何引用动态创建的Raphael画布?

如何引用动态创建的Raphael画布?
EN

Stack Overflow用户
提问于 2011-04-24 08:59:30
回答 1查看 2.3K关注 0票数 2

我正在创建一个jQuery插件,它可以动态地创建Raphael对象,假设您可以.

代码语言:javascript
运行
复制
$("div").draw({
  type: 'circle',
  radius: '10',
  color: '#000'
})

以及插件代码(仅举一个例子):

代码语言:javascript
运行
复制
  $.fn.draw = function( options ) { 
  //settings/options stuff


   var width = $(this).width();
   var height = $(this).height();
   var widget = Raphael($(this)[0], width, height);
   var c = widget.circle(...).attr({...})

   //saving the raphael reference in the element itself
   $(this).data('raphael', {
     circle : c
   })

}

但是,我希望能够更新这样的元素:

代码语言:javascript
运行
复制
$("div").draw({
  type: 'update',
  radius: '20',
  color: '#fff'
});

我可以“拯救”执行$(this).data().raphael.circle的对象,但是它拒绝动画,我知道它是一个raphael对象,因为它甚至有动画proto,但是它产生了一个未定义的TypeError:无法读取属性'0‘)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-24 18:52:55

我尝试了您的代码,做了一些修改,$(this).data().raphael.circle做了动画。这就是我所做的(我知道这和你的不完全一样,但给出了要点)

代码语言:javascript
运行
复制
   $.fn.draw = function( options ) { 
  //settings/options stuff

       if(options.type === 'draw') {
         var width = $(this).width();
         var height = $(this).height();
         var widget = Raphael($(this)[0], width, height);
         var c = widget.circle(100, 100, 50);

         //saving the raphael reference in the element itself
         $(this).data('raphael', {
             circle : c
         });
//$(this).data().raphael.circle.animate() does not work here

        }

        else if(options.type === 'update') {
            $(this).data().raphael.circle.animate({cx: 200, cy: 200});
            //But this works here
        }

}

在本例中,使用$( this ).data().raphael.circle引用元素确实有效,但只在else if中工作。我不知道为什么。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5769630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档