ericdrowell / KineticJS

KineticJS is an HTML5 Canvas JavaScript framework that extends the 2d context by enabling canvas interactivity for desktop and mobile applications.
http://www.kineticjs.com
3.98k stars 753 forks source link

Image Drag function:how to control the image's direction 'vertical' or 'horizontal' when dragging? #1059

Closed zuoRambo closed 9 years ago

zuoRambo commented 9 years ago

my kinetic version is '5.1.0'i find the follow code from src file kinetic-v5.1.0.js

     * // create vertical drag and drop<br>
     * node.dragBoundFunc(function(){<br>
     *   return {<br>
     *     x: this.getAbsolutePosition().x,<br>
     *     y: pos.y<br>
     *   };<br>
     * });

and there is my function code, i want to achieve the goal to press 'shift' or 'alt' key to control the direction

var image = new Kinetic.Image(conf);
image.on('mousedown',function(e){
        if(e.evt.altKey)
            this.dragBoundFunc(function(pos){
                return {
                    x: pos.x,
                    y: this.getAbsolutePosition().y,
                }
            });
        if(e.evt.shiftKey)
            this.dragBoundFunc(function(pos){
                return {
                    x: this.getAbsolutePosition().x,
                    y: pos.y,
                };
            });
    });

but unlucky! the code didn't work well! could you help me? where can i find the api wiki about the kinetic?

lavrton commented 9 years ago

Works nice for me: http://jsbin.com/bipoke/13/edit?js,output

zuoRambo commented 9 years ago

thank you a lot, @lavrton man, even your code is not none of the bussiness about the 'Image drag funciton' . i have find the solution how to fix my bug!do you have an api documention about kinetic js?

var image = new Kinetic.Image(conf);
image.on('mousedown',function(e){
    t.isImageDrage = true;
    this.dragBoundFunc(function(pos){
        if(e.evt.altKey)
            return {
                x: pos.x,
                y: this.getAbsolutePosition().y,
            }
        else if(e.evt.shiftKey)
            return {
                x: this.getAbsolutePosition().x,
                y: pos.y,
            }
        else
            return pos;
    });
});

thanks again for your help!

lavrton commented 9 years ago

Now sure how long it will be available here: http://lavrton.github.io/KineticJS/api/ But I suggest you to switch to KineticJS fork: http://konvajs.github.io/

zuoRambo commented 9 years ago

@lavrton wow! the api is amazing! how do you make it? jsDOC_toolkit?

lavrton commented 9 years ago

@zuoRambo right. + template https://github.com/terryweiss/docstrap