Library globals

Source canvas . api . image.nas

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#-------------------------------------------------------------------------------
# canvas.Image
#-------------------------------------------------------------------------------
# Class for an image element on a canvas
#
var Image = {
    new: func(ghost) {
        var obj = {
            parents: [Image, Element.new(ghost)],
        };
        return obj;
    },

    # Set image file to be used
    #
    # @param file Path to file or canvas (Use canvas://... for canvas, eg.
    #                         canvas://by-index/texture[0])
    setFile: func(file) {
        me.set("src", file);
    },

    # Set rectangular region of source image to be used
    #
    # @param left       Rectangle minimum x coordinate
    # @param top        Rectangle minimum y coordinate
    # @param right      Rectangle maximum x coordinate
    # @param bottom     Rectangle maximum y coordinate
    # @param normalized Whether to use normalized ([0,1]) or image
    #                   ([0, image_width]/[0, image_height]) coordinates
    setSourceRect: func {
        # Work with both positional arguments and named arguments.
        # Support first argument being a vector instead of four separate ones.
        if (size(arg) == 1) {
            arg = arg[0];
        }
        elsif (size(arg) and size(arg) < 4 and isvec(arg[0])) {
            arg = arg[0]~arg[1:];
        }
        if (!contains(caller(0)[0], "normalized")) {
            if (size(arg) > 4)
                var normalized = arg[4];
            else var normalized = 1;
        }
        if (size(arg) >= 3)
            var (left,top,right,bottom) = arg;

        me._node.getNode("source", 1).setValues({
            left: left,
            top: top,
            right: right,
            bottom: bottom,
            normalized: normalized
        });
        return me;
    },

    # Set size of image element
    #
    # @param width
    # @param height
    # - or -
    # @param size ([width, height])
    setSize: func {
        me._node.setValues({size: _arg2valarray(arg)});
        return me;
    }
};