首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在arbor.js中使用映像时出现的性能问题

在arbor.js中使用映像时出现的性能问题
EN

Stack Overflow用户
提问于 2012-03-23 00:12:31
回答 1查看 3.6K关注 0票数 6

我一直致力于使arbor.js适应图像的使用。

然而,作为一个相对的JS新手,我所拥有的是完全没有优化的。

据我所知,我设置它的方式是为每一张图像和每一帧重新创建图像对象,导致大量闪烁。

有没有人能建议一种方法,将新的Image()内容从重绘函数移到初始化函数中?据我所知,这是一个基本的OOP问题,但完全卡住了。

谢谢!

输出脚本中我所处位置的Pastebin

Current status

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-03 07:01:26

向所有人道歉!有几个步骤。我将重点介绍关键阶段,其余部分来自本教程。

首先,将相关信息添加到您的JSON中,例如:

代码语言:javascript
复制
nodes:{
    innovation:{    'color':colour.darkblue, 
                    'shape':'dot', 
                    'radius':30, 
                    'image': 'innovation.png',
                    'image_w':130,
                    'image_h':24,
                    'alpha':1 },
    participation:{ 'color':colour.purple, 
                    'shape':'dot',
                    'radius':40, 
                    'image':'participation.png',
                    'image_w':130,
                    'image_h':24,
                    'alpha':1 },
   ...

当这个东西加载时,缓存所有的图像。

代码语言:javascript
复制
init:function(system){

    // Normal initialisation
    particleSystem = system
    particleSystem.screenSize(canvas.width, canvas.height)
    particleSystem.screenPadding(25, 50)
    that.initMouseHandling()

    // Preload all images into the node object
    particleSystem.eachNode(function(node, pt) {
        if(node.data.image) {
            node.data.imageob = new Image()
            node.data.imageob.src = imagepath + node.data.image
        }
    })
 ...

然后,为了移动图像本身。

代码语言:javascript
复制
particleSystem.eachNode(function(node, pt){
    ...       
    // Image info from JSON
    var imageob = node.data.imageob
    var imageH = node.data.image_h
    var imageW = node.data.image_w
    ...
    // Draw the object        
    if (node.data.shape=='dot'){
        // Check if it's a dot
        gfx.oval(pt.x-w/2, pt.y-w/2, w,w, {fill:ctx.fillStyle, alpha:node.data.alpha})
        nodeBoxes[node.name] = [pt.x-w/2, pt.y-w/2, w,w]
        // Does it have an image?      
        if (imageob){
            // Images are drawn from cache
            ctx.drawImage(imageob, pt.x-(imageW/2), pt.y+radius/2, imageW, imageH)
        }
    }else {
        // If none of the above, draw a rectangle
        gfx.rect(pt.x-w/2, pt.y-10, w,20, 4, {fill:ctx.fillStyle, alpha:node.data.alpha})
        nodeBoxes[node.name] = [pt.x-w/2, pt.y-11, w, 22]
    }
    ...
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9826253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档