Raphel image drag 拖动

本文介绍如何利用Raphael.js库实现图形元素的拖拽交互功能,包括使用circle、image等不同类型的元素,并提供了详细的源代码解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<script>
 window.onload = function () {
 var R = Raphael(0, 0, "100%", "100%"),
 r = R.circle(100, 100, 50).attr({fill: "hsb(0, 1, 1)", stroke: "none", opacity: .5}),
 g = R.circle(210, 100, 50).attr({fill: "hsb(.3, 1, 1)", stroke: "none", opacity: .5}),
 b = R.circle(320, 100, 50).attr({fill: "hsb(.6, 1, 1)", stroke: "none", opacity: .5}),
 p = R.circle(430, 100, 50).attr({fill: "hsb(.8, 1, 1)", stroke: "none", opacity: .5});
 var start = function () {
 this.ox = this.attr("cx");
 this.oy = this.attr("cy");
 this.animate({r: 70, opacity: .25}, 500, ">");
 },
 move = function (dx, dy) {
 this.attr({cx: this.ox + dx, cy: this.oy + dy});
 },
 up = function () {
 this.animate({r: 50, opacity: .5}, 500, ">");
 };
 R.set(r, g, b, p).drag(move, start, up);
 };
 </script>
以上是官方拖动的源代码,可惜是circle

而你要拖动的可能是矩形和image,他的坐标不是cx cy 所以要把cx cy改成xy

 window.onload = function () {
         var R = Raphael(document.getElementById('canvas_container'),"1000", "1000");
             // r = R.circle(100, 100, 50).attr({fill: "hsb(0, 1, 1)", stroke: "none", opacity: .5}),
             //g = R.circle(210, 100, 50).attr({fill: "hsb(.3, 1, 1)", stroke: "none", opacity: .5}),
             //b = R.circle(320, 100, 50).attr({fill: "hsb(.6, 1, 1)", stroke: "none", opacity: .5}),
             //p = R.circle(430, 100, 50).attr({fill: "hsb(.8, 1, 1)", stroke: "none", opacity: .5});
         var cc=R.image("host1.png",30,40,120,120).attr({
           stroke: "none",
           opacity: .5,
           cursor:"pointer"
        });
         start = function () {
             this.ox = this.attr("x");
             this.oy = this.attr("y");
             this.animate({r: 70, opacity: .25}, 500, ">");
         },
         move = function (dx, dy) {
             this.attr({x: this.ox + dx, y: this.oy + dy});
         },
         up = function () {
             this.animate({r: 50, opacity: .5}, 500, ">");
         };
          //R.image("host1.png", 800, 170, 72, 72).drag(move, start, up);
          R.set(cc).drag(move, start, up);
     };


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值