【问题标题】:KineticJS: How to Cache a Group of Shapes?KineticJS:如何缓存一组形状?
【发布时间】:2014-12-01 18:14:39
【问题描述】:

我想提高画布对象的移动拖放性能。我有一个包含不同形状(图像、星星、Recs、...)的组。当我拖放或旋转整个组时,我想使用缓存来提高性能。

这是我创建的 JSFiddle:http://jsfiddle.net/confile/k4Lsv73d/

function setFPSMeter() {
    var RAF = (function () {
        return window["requestAnimationFrame"] || window["webkitRequestAnimationFrame"] || window["mozRequestAnimationFrame"] || window["oRequestAnimationFrame"] || window["msRequestAnimationFrame"] || FRAF;
    })();

    function FRAF(callback) {
        window.setTimeout(callback, 1000 / 60);
    }

    var fpsDiv = document.createElement("div");
    fpsDiv.style.position = "absolute";
    fpsDiv.style.zIndex="40000";
    document.body.appendChild(fpsDiv);

    var meter = new window.FPSMeter(fpsDiv, {
        position: 'fixed',
        zIndex: 10,
        right: '5px',
        top: '5px',
        left: 'auto',
        bottom: 'auto',
        margin: '0 0 0 0',
        interval: 1000,
        graph: true,
        history: 20,
        theme: 'colorful',
        heat: true
    });
    var tick = function () {
        meter.tick();
        RAF(tick);
    };
    tick();
}

setFPSMeter();

console.log(!!window.FPSMeter);

Kinetic.pixelRatio = 1;
//console.log(window.requestAnimationFrame);
// http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.1.0.min.js

var width = $("#container").width();
var height = $("#container").height();
console.log("width: "+width+" height: "+height);

var stage = new Kinetic.Stage({
    container: 'container',
    width: width,
    height: 400
});

var layer = new Kinetic.Layer();
// add the layer to the stage
stage.add(layer);


var blob;
var imageObj = new Image();
imageObj.onload = function () {

    var group = new Kinetic.Group({
        draggable: true
    });

    var star = new Kinetic.Star({
        innerRadius: 50,
        outerRadius: 70,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 5,
        numPoints: 20,
        x: 200,
        y: 100,
        shadowOffset: 5,
        shadowColor: 'black',
        shadowBlur: 5,
        shadowOpacity: 0.5
      });

    blob = new Kinetic.Image({
        image: imageObj,
        x: 50,
        y: 40,
     //   draggable: true,
        width: 300,
        height: 300
    });

    // performance
    // blob.transformsEnabled('position');
    group.add(blob);
    group.add(star);
    //  setTimeout(function () {
    // add the shape to the layer
    //layer.add(blob);
    layer.add(group);
   // blob.cache();

    layer.batchDraw();
    //  }, 50);


    loadEvents();

    /*
    blob.cache({
        width: 500,
        height: 500,
        drawBorder: true
      }).offset({
        x: 10,
        y: 10
      });
     */

    /*
    blob.cache({
        drawBorder: true
        });
    */


};

imageObj.src = "https://dl.dropboxusercontent.com/u/47067729/sandwich2.svg";


function getDistance(p1, p2) {
        return Math.sqrt(Math.pow((p2.x - p1.x), 2) + Math.pow((p2.y - p1.y), 2));
      }

function loadEvents() {
    var lastDist = 0;
    var startScale = 1;

    stage.getContent().addEventListener('touchstart', function (evt) {
        console.log("touchstart");
        blob.clearCache();
        blob.cache();
    }, false);

    stage.getContent().addEventListener('touchmove', function (evt) {
     clearCache
        var touch1 = evt.touches[0];
        var touch2 = evt.touches[1];

        if (touch1 && touch2) {

           // blob.draggable(false);

            var dist = getDistance({
                x: touch1.clientX,
                y: touch1.clientY
            }, {
                x: touch2.clientX,
                y: touch2.clientY
            });


            if (lastDist == 0) {
                lastDist = dist;
            }
            console.log("touchmove dist: "+dist);
            console.log("touchmove lastDist: "+lastDist);
            console.log("blob.getScale().x: "+blob.getScale().x);
            // scale

            var scale = blob.getScale().x * dist / lastDist;
            console.log("scale: "+scale);

            blob.setScale(scale);

            lastDist = dist;

            //// rotate




            ///

            layer.batchDraw();
        }
    }, false);

    stage.getContent().addEventListener('touchend', function (evt) {
        console.log("touchend");
       // blob.draggable(true);
        lastDist = 0;
    }, false);


}

如何在 KineticJS 中对一组形状使用缓存?

【问题讨论】:

    标签: javascript html kineticjs


    【解决方案1】:
    group.cache({
        width: blob.width(),
        height: blob.height(),
        x : blob.x(),
        y : blob.y(),
        drawBorder: true
      }).offset({
        x: 10,
        y: 10
      });
    

    http://jsfiddle.net/k4Lsv73d/2/

    您最好配置xywidthheight attrs 用于缓存。

    【讨论】:

    • 我是否需要在某个时候清除缓存?当我缩放组时,我必须再次使用 neu 宽度和高度执行 cache() 吗?
    • 取决于您的要求。如果您缩放(例如 2 倍)缓存组,您的形状将变得模糊。
    • 当我的画布上有多个形状组时,我应该在每一个上都使用 cache() 吗?我之前要申请 clearCache() 吗?
    • 你可以在任何你想要的地方使用cache。缓存只是转换为位图图像。不需要使用clearCache()
    猜你喜欢
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 2014-08-31
    • 2014-07-26
    相关资源
    最近更新 更多