paint application with kinetic js

。_饼干妹妹 提交于 2019-12-08 11:56:25

问题


I have made a drag and drop application in HTML5 canvas with kinetic js. Can we also add the paint brush functionality to the same canvas using kinetic js? If yes, please share the link for one such application, and also the code, if possible.


回答1:


You can use mouse events to let the user create a sketch on the canvas.

Here's how to let the user create a Kinetic polyline.

On mousedown:

  • Set a mousedown flag to true (indicating that the user is sketching)
  • Create a new Kinetic Line object

On mousemove:

  • Add the current mouse position to the points in the line object
  • Redraw the line that now includes the latest mouse position

On mouseup:

  • Clear the mousedown flag.

Repeat every time the user sketches a new polyline.

To let the user draw other Kinetic shapes (rect,circle,etc.) you have many options:

Have the user select which shape they want to create. Use mousedown + mouseup to get the bounds of the shape they want. Then add that kinetic shape with those bounds to the stage.

OR

Have the user select which shape they want to create. Create a generic version of that shape and put it on the stage. Let the user drag the generic shape to their desired position. Let the user customize that generic shape by dragging the bounds anchors.

OR

Have the user select which shape they want to create and have them text input the bounds. Create a generic version of that shape and put it on the stage. Let the user drag the generic shape to their desired position.

Really, there are so many options that the design is up to you.

Here is code and a Fiddle: http://jsfiddle.net/m1erickson/WW3sK/

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Prototype</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.3.3-beta.js"></script>

<style>
#container{
  border:solid 1px #ccc;
  margin-top: 10px;
}
</style>        
<script>
$(function(){

        // create a stage and a layer
        var stage = new Kinetic.Stage({
            container: 'container',
            width: 400,
            height: 400
        });
        var layer = new Kinetic.Layer();
        stage.add(layer);

        // an empty stage does not emit mouse-events
        // so fill the stage with a background rectangle
        // that can emit mouse-events
        var background = new Kinetic.Rect({
            x: 0,
            y: 0,
            width: stage.getWidth(),
            height: stage.getHeight(),
            fill: 'white',
            stroke: 'black',
            strokeWidth: 1,
        })        
        layer.add(background);
        layer.draw();

        // a flag we use to see if we're dragging the mouse
        var isMouseDown=false;
        // a reference to the line we are currently drawing
        var newline;
        // a reference to the array of points making newline
        var points=[];

        // on the background
        // listen for mousedown, mouseup and mousemove events
        background.on('mousedown', function(){onMousedown();});
        background.on('mouseup', function(){onMouseup();});
        background.on('mousemove', function(){onMousemove();});

        // On mousedown
        // Set the isMouseDown flag to true
        // Create a new line,
        // Clear the points array for new points
        // set newline reference to the newly created line
        function onMousedown(event) {
            isMouseDown = true;
            points=[];
            points.push(stage.getMousePosition());
            var line = new Kinetic.Line({
                points: points,
                stroke: "green",
                strokeWidth: 5,
                lineCap: 'round',
                lineJoin: 'round'
            });
            layer.add(line);
            newline=line;
        }

        // on mouseup end the line by clearing the isMouseDown flag
        function onMouseup(event) {
            isMouseDown=false;
        }

        // on mousemove
        // Add the current mouse position to the points[] array
        // Update newline to include all points in points[]
        // and redraw the layer
        function onMousemove(event) {
            if(!isMouseDown){return;};
            points.push(stage.getMousePosition());
            newline.setPoints(points);
            layer.drawScene();
        }


}); // end $(function(){});

</script>       
</head>

<body>
    <div id="container"></div>
</body>
</html>



回答2:


I have done something some weeks before. I don't know if it can help you.

http://jsfiddle.net/F3zwW/10/

var x;
var y;
var entry;
var isFinished = false;
circle.on('dragstart', function(evt) {
    entry = new Kinetic.Circle({
        x: evt.x,
        y: evt.y,
        radius: 10,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 2
    });
    group.add(entry);
    layer.add(group);
    entry.moveToTop();
});


circle.on('dragmove', function(evt) {
    if (isFinished) return;
    if (x != undefined && y != undefined) {
        var line = new Kinetic.Line({
            points: [x, y, evt.x, evt.y],
            stroke: 'red',
            strokeWidth: 20,
            lineCap: 'round',
            lineJoin: 'round'
        });
        length +=  Math.sqrt(Math.pow(evt.x - x, 2) + Math.pow(evt.y - y, 2));
        group.add(line);
    }
    x = evt.x;
    y = evt.y;
    layer.add(group);
    circle.moveToTop();
    entry.moveToTop();
    layer.draw();
    if (length > 120) circle.fire('dragend');

});

circle.on('dragend', function(evt) {
    if (isFinished) return;

    var exit = new Kinetic.Circle({
        x: x,
        y: y,
        radius: 10,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 2
    });
    group.add(exit);
    layer.add(group);
    circle.hide();
    layer.draw();
    isFinished = true;
});

Is that the behavior you are looking for ? Here I wanted for some reasons to limit the length but you can easily remove this restriction.



来源:https://stackoverflow.com/questions/16708016/paint-application-with-kinetic-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!