Glimmr Canvas-Based Drawing

version 2/101030 by Erik Temple

  • Home page
  • Beginning
  • Previous
  • Next



  • Section - Centering algorithm for sprites

    [When you create a new class of g-element, it is good practice to define a centering phrase that will be able to find the center point, in canvas coordinates, of any object of that class. This should not use the win-x/win-y properties, or any other property that is set by the element scaling rules. For this reason, the algorithm should assume that the origin is the upper-left hand corner (center and right alignment are calculated by the element scaling rules).]

    To decide what list of numbers is the center-point of (current-sprite - a sprite):
        let dx be a number;
        let dy be a number;
        let x be entry 1 of the origin of current-sprite;
        let y be entry 2 of the origin of current-sprite;
        let x-temp be the image-width of the image-ID of current-sprite as a fixed point number;
        let y-temp be the image-height of the image-ID of current-sprite as a fixed point number;
        unless the asymmetrical scaling option is active:
            let dx be x-temp real times the scaling factor of the current-sprite as an integer;
            let dy be y-temp real times the scaling factor of the current-sprite as an integer;
        otherwise:
            let dx be x-temp real times the x-scaling factor of the current-sprite as an integer;
            let dy be y-temp real times the y-scaling factor of the current-sprite as an integer;
        let x be x + (dx / 2);
        let y be y + (dy / 2);
        let coord be {0, 0};
        now entry 1 of coord is x;
        now entry 2 of coord is y;
        decide on coord.