[Dojo-interest] Is this at all possible with dojo.gfx ordojoingeneral?

Peter Svensson psvensson at gmail.com
Thu Apr 24 16:50:24 UTC 2008


Craig, you're absolutely right. That's a very good idea. Take the file
Spring.js, for instance;

/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Kyle Scholz      http://kylescholz.com/
 * Copyright: 2006-2007
 */

/**
 * Spring
 *
 * @author Kyle Scholz
 *
 * @version 0.3
 *
 * A Spring Force between two Particle.
 *
 * @param {Particle} a  - A Particle.
 * @param {Particle} b  - The other Partilce.
 * @param {Number} springConstant - The Spring constant.
 * @param {Number} dampingConstant  - The damping constant.
 * @param {Number} r  - The rest length of the Spring.
 */
var Spring = function( a, b, springConstant, dampingConstant, restLength ) {
    this.init( a, b, springConstant, dampingConstant, restLength );
}
Spring.prototype = {

    /*
     * Initialize the Spring Force.
     *
     * @param {Particle} a  - A Particle.
     * @param {Particle} b  - The other Partilce.
     * @param {Number} springConstant - The Spring constant.
     * @param {Number} dampingConstant  - The damping constant.
     * @param {Number} restLength  - The length of the Spring at rest.
     */
    init: function( a, b, springConstant, dampingConstant, restLength ) {
        this['springConstant'] = springConstant;

        this['damping'] = dampingConstant;

        this['restLength'] = restLength;

        this['a'] = a;

        this['b'] = b;

        // The force exerted by the Spring on the X axis.
        this.forceX = 0;

        // The force exerted by the Spring on the Y axis.
        this.forceY = 0;
    },

    /*
     * Apply a spring force based on distance between particles.
     */
    apply: function() {

        // Determine the current length of the spring
        var dx = this.a.positionX - this.b.positionX;
        var dy = this.a.positionY - this.b.positionY;
        var springLength = Math.sqrt( dx*dx + dy*dy );

        if ( springLength == 0 ) {
            dx = 0;
            dy = 0;
        } else {
            dx *= (1/springLength);
            dy *= (1/springLength);
        }

        // Determine the spring force
        var springForce = -(springLength - this.restLength) *
this.springConstant;

        // Determine the damping force
        var vx = this.a.velocityX - this.b.velocityX;
        var vy = this.a.velocityY - this.b.velocityY;
        var dampingForce = -this.damping * (dx * vx + dy * vy);

        // Determine the sum force
        var force = springForce + dampingForce;

        // Apply force to vectors
        dx *= force;
        dy *= force;

        // Get the difference since last application
        var dfx = dx - this.forceX;
        var dfy = dy - this.forceY;

        if (!this.a.fixed && !this.a.selected) {
            this.a.forceX += dfx;
            this.a.forceY += dfy;
        }

        if (!this.b.fixed && !this.b.selected) {
            this.b.forceX -= dfx;
            this.b.forceY -= dfy;
        }

        // Update the spring force
        this.forceX = dx;
        this.forceY = dy;
    }
}


This could probably be integrated with dojox.fx, I think

Cheers,
PS

On Thu, Apr 24, 2008 at 5:57 PM, Craig L. Ching <cching at mqsoftware.com>
wrote:

> > What I would recommend to do first is to take the base file and create
> a
> > dijit out of it. take a small custom dijit example, and convert all
> > function definitions from foo.prototype.bar = function() .. to   bar:
> > function(){ .. },  ...
> >
>
> Just a quick comment, I'm assuming that there are some good
> force-directed graph layout algorithms in the code (I haven't looked at
> it at all, so pardon me if I'm wrong), wouldn't those go well in a
> separate dojox project?  I'm asking because I recently wrote a
> force-directed graph layout implementation for use with dojo, but I'd
> love to use this instead.
>
> A dijit would be wonderful, but I'm guessing there are some other
> reusable components under the hood.
>
> > Cheers,
> > PS
>
> Cheers,
> Craig
> _______________________________________________
> FAQ: http://dojotoolkit.org/support/faq
> Book: http://dojotoolkit.org/docs/book
> Forums: http://dojotoolkit.org/forum
> Dojo-interest at dojotoolkit.org<http://dojotoolkit.org/forumDojo-interest@dojotoolkit.org>
> http://turtle.dojotoolkit.org/mailman/listinfo/dojo-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://turtle.dojotoolkit.org/pipermail/dojo-interest/attachments/20080424/c0ba3658/attachment-0001.html 


More information about the Dojo-interest mailing list