首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >physicsJS和位图

physicsJS和位图
EN

Stack Overflow用户
提问于 2013-10-07 21:22:34
回答 1查看 622关注 0票数 0

有一个新的物理库http://wellcaffeinated.net/PhysicsJS/我想玩它和使用位图而不是“画布路径”。在https://github.com/wellcaffeinated/PhysicsJS/blob/master/examples/physicsjs-full.js中有这个函数

代码语言:javascript
复制
drawCircle: function(x, y, r, styles, ctx){

            ctx = ctx || this.ctx;

            ctx.beginPath();
            this.setStyle( styles, ctx );
            ctx.arc(x, y, r, 0, Pi2, false);
            ctx.closePath();
            ctx.stroke();
            ctx.fill();
           /*

           var i = new Image();
           i.src = "http://www.pngfactory.net/_png/_thumb/19626-bubka-R2D2.png";
           i.onload = function() {
           console.log("load");
           ctx.drawImage(i, 0, 0);
           };
            */
    }

但是当我在画布、img.src和load中添加img时,…没有发生任何事情。

这里有一个“正式”的jsfiddle http://jsfiddle.net/wellcaffeinated/KkDb6/和一个wiki https://github.com/wellcaffeinated/PhysicsJS/wiki/Fundamentals

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-13 11:55:06

以下是PhysicsJS:http://jsfiddle.net/wellcaffeinated/mpbNF/作者提出的解决方案

代码语言:javascript
复制
/**
 * PhysicsJS by Jasper Palfree <wellcaffeinated.net>
 * http://wellcaffeinated.net/PhysicsJS
 *
 * Simple Spin example for PhysicsJS
 */
Physics(function(world){

    var viewWidth = 500;
    var viewHeight = 300;

    var renderer = Physics.renderer('canvas', {
        el: 'viewport',
        width: viewWidth,
        height: viewHeight,
        meta: false, // don't display meta data
        styles: {
            // set colors for the circle bodies
            'circle' : {
                strokeStyle: 'hsla(60, 37%, 17%, 1)',
                lineWidth: 1,
                fillStyle: 'hsla(60, 37%, 57%, 0.8)',
                angleIndicator: 'hsla(60, 37%, 17%, 0.4)'
            }
        }
    });

    // add the renderer
    world.add( renderer );
    // render on each step
    world.subscribe('step', function(){
        world.render();
    });

    // bounds of the window
    var viewportBounds = Physics.aabb(0, 0, viewWidth, viewHeight);

    // constrain objects to these bounds
    world.add(Physics.behavior('edge-collision-detection', {
        aabb: viewportBounds,
        restitution: 0.99,
        cof: 0.99
    }));

    Physics.body('wheel', 'circle', function( parent ){

        return {
            // no need for an init

            // spin the wheel at desired speed
            spin: function( speed ){
                // the wheels are spinning...
                this.state.angular.vel = speed;
            }
        };
    });

    var myWheel = Physics.body('wheel', {
        x: 40,
        y: 30,
        radius: 45
    });

    // Not a hack
    myWheel.view = new Image();
    myWheel.view.src = 'http://www.large-icons.com/stock-icons/free-large-twitter/round_button-icon.gif';

    world.add( myWheel );

    // for example, use jquery to listen for a button click, and spin the wheel on the next step
    $('button').on('click', function(){
        // wait for the next step before spinning the wheel
        world.subscribe('step', function( data ){
            myWheel.spin( 0.03 );
            // only execute callback once
            world.unsubscribe( 'step', data.handler );
        });
    });

    // ensure objects bounce when edge collision is detected
    world.add( Physics.behavior('body-impulse-response') );

    // add some gravity
    world.add( Physics.behavior('constant-acceleration') );

    // subscribe to ticker to advance the simulation
    Physics.util.ticker.subscribe(function( time, dt ){

        world.step( time );
    });

    // start the ticker
    Physics.util.ticker.start();

});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19235028

复制
相关文章

相似问题

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