How to scale a shape from its center as origin?

ilgaar

I have the following, code, that works perfectly fine, except that it scales the shape from top left corner as origin, but I want it to scale from its center as origin. How should do that in away, the animation will start to scale up from center of the shape when moused over, and scale down to original size again from center as origin?

    <div id="hex-menu"></div>
    <script src="js/kinetic.min.js" charset="utf-8"></script>
    <script defer="defer" type="text/javascript">
        function makeHex(x, y, fill) {
            var hex = new Kinetic.Shape({
                x: x,
                y: y,
                fill: fill,
                // a Kinetic.Canvas renderer is passed into the drawFunc function
                drawFunc: function (canvas) {
                    var context = canvas.getContext();
                    context.save();
                    context.translate(x, y);
                    context.beginPath();
                    context.moveTo(0.1, 51.9);
                    context.bezierCurveTo(0.1, 43.6, 4.6, 35.8, 11.9, 31.6);
                    context.lineTo(61.0, 3.3);
                    context.bezierCurveTo(68.2, -0.9, 77.2, -0.9, 84.4, 3.3);
                    context.lineTo(133.6, 31.6);
                    context.bezierCurveTo(140.8, 35.8, 145.3, 43.6, 145.3, 52.0);
                    context.lineTo(145.3, 108.7);
                    context.bezierCurveTo(145.3, 117.1, 140.8, 124.8, 133.6, 129.0);
                    context.lineTo(84.4, 157.4);
                    context.bezierCurveTo(77.2, 161.5, 68.2, 161.5, 61.0, 157.4);
                    context.lineTo(11.9, 129.0);
                    context.bezierCurveTo(4.6, 124.8, 0.1, 117.0, 0.1, 108.7);
                    context.lineTo(0.1, 51.9);
                    context.closePath();
                    canvas.fillStroke(this);
                    context.restore();
                }
            });
            return hex;
        }
        function Zoom(node) {
            var tween = new Kinetic.Tween({
                node: node,
                duration: 0.5,
                scaleX: 1.2,
                scaleY: 1.2
            });
        return tween;
        }
        function AddAnimation(node, tween) {
            node.on('mouseover', function() {
                tween.play();
            });
            node.on('mouseleave', function() {
                tween.reverse();
            });
        }
        var stage = new Kinetic.Stage({
            container: 'hex-menu',
            width: 513,
            height: 484
        });
        var shapesLayer = new Kinetic.Layer();
        var home = makeHex(80, 90, 'rgb(19, 217, 209)', 50);
        shapesLayer.add(home);
        stage.add(shapesLayer);
        var zoomHome = Zoom(home);
        AddAnimation(home, zoomHome);
    </script>
projeqht

You need to set the offset property to the centre of the shape using some trigonometry. By default Kinetic.Circle and Kinetic.Ellipse are offset in the centre, while the rest of the shapes are offset on the top left.

In this example I'm setting the offset to half of the width and height (50 and 25), but you'll need to do some calculations on your hex object to get the centre point.

var rect = new Kinetic.Rect({
  x: 239,
  y: 75,
  width: 100,
  height: 50,
  fill: 'green',
  stroke: 'black',
  strokeWidth: 4,
  offset: {x: 50, y: 25}
});

Or you can use the methods getOffset() and setOffset()

http://kineticjs.com/docs/Kinetic.Shape.html#getOffset http://kineticjs.com/docs/Kinetic.Shape.html#setOffset

JSFIDDLE

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

jquery scale a div to the center of its parent div

分類Dev

How to Rotate a GameObject around its center, from script?

分類Dev

How to apply mask to a tensor and keep its original shape

分類Dev

SVG animation: scale multiple figures from their respective origin in a single .svg file

分類Dev

How to remove commits from GIT origin?

分類Dev

How to remove Media Center from Windows 8

分類Dev

How to draw a rectangle and adjust its shape by drag and drop in PyQt5

分類Dev

How to unnest (unlevel) unnecessary nested list without changing its shape? (Python)

分類Dev

Align shape to center of textview on Relative layout

分類Dev

How to get touched shape (arc) from Android canvas?

分類Dev

how to shape tensor from fully connected to to 4-D

分類Dev

how to split a pandas dataframe from wide to tall shape

分類Dev

How to construct a tree of particular shape with elements from a list

分類Dev

How to center an absolutely positioned element within its parent when the child element's height is unknown

分類Dev

Java Swing - Clicking on a shape to change its color

分類Dev

Google 3D Earth How to find the center point from all the markers and set center view

分類Dev

How to center images in gif using convert from imagamagick?

分類Dev

How to make a shape in swift

分類Dev

How to scale a number on 1-100 scale

分類Dev

UIView's frame, bounds, center, origin, when to use what?

分類Dev

How to Scale Azure Webjobs

分類Dev

How is the MTR scale calculated?

分類Dev

Bypass reCAPTCHA from a specified origin

分類Dev

How can I get the latest VLC if its not available from the VLC download page and its PPA

分類Dev

How can I get the latest VLC if its not available from the VLC download page and its PPA

分類Dev

How to prevent SVG text from flowing out of its circle?

分類Dev

How to return different types from an if and its corresponding else in DAML?

分類Dev

How to recursively download a web page and its linked content from a URL?

分類Dev

BreezeJS - How remove a navigational property from its parent?

Related 関連記事

  1. 1

    jquery scale a div to the center of its parent div

  2. 2

    How to Rotate a GameObject around its center, from script?

  3. 3

    How to apply mask to a tensor and keep its original shape

  4. 4

    SVG animation: scale multiple figures from their respective origin in a single .svg file

  5. 5

    How to remove commits from GIT origin?

  6. 6

    How to remove Media Center from Windows 8

  7. 7

    How to draw a rectangle and adjust its shape by drag and drop in PyQt5

  8. 8

    How to unnest (unlevel) unnecessary nested list without changing its shape? (Python)

  9. 9

    Align shape to center of textview on Relative layout

  10. 10

    How to get touched shape (arc) from Android canvas?

  11. 11

    how to shape tensor from fully connected to to 4-D

  12. 12

    how to split a pandas dataframe from wide to tall shape

  13. 13

    How to construct a tree of particular shape with elements from a list

  14. 14

    How to center an absolutely positioned element within its parent when the child element's height is unknown

  15. 15

    Java Swing - Clicking on a shape to change its color

  16. 16

    Google 3D Earth How to find the center point from all the markers and set center view

  17. 17

    How to center images in gif using convert from imagamagick?

  18. 18

    How to make a shape in swift

  19. 19

    How to scale a number on 1-100 scale

  20. 20

    UIView's frame, bounds, center, origin, when to use what?

  21. 21

    How to Scale Azure Webjobs

  22. 22

    How is the MTR scale calculated?

  23. 23

    Bypass reCAPTCHA from a specified origin

  24. 24

    How can I get the latest VLC if its not available from the VLC download page and its PPA

  25. 25

    How can I get the latest VLC if its not available from the VLC download page and its PPA

  26. 26

    How to prevent SVG text from flowing out of its circle?

  27. 27

    How to return different types from an if and its corresponding else in DAML?

  28. 28

    How to recursively download a web page and its linked content from a URL?

  29. 29

    BreezeJS - How remove a navigational property from its parent?

ホットタグ

アーカイブ