svghmi/widget_animate.ysl2
author Edouard Tisserant
Tue, 26 Jan 2021 11:17:08 +0100
branchsvghmi
changeset 3119 17a9c7a334f7
parent 3064 4b44d09c48a7
child 3232 7bdb766c2a4d
permissions -rw-r--r--
SVGHMI: Fix browser side exception when some widget are not used, and are then discarded and not present in final SVG. In that case JS code was still making reference to discarded widget elements and was raising exception at init.
// widget_animate.ysl2

template "widget[@type='Animate']", mode="widget_class"{
    ||
    class AnimateWidget extends Widget{
        frequency = 5;
        speed = 0;
        start = false;
        widget_center = undefined;

        dispatch(value) {
            this.speed = value / 5;

            //reconfigure animation
            this.request_animate();
        }

        animate(){
           // change animation properties
           for(let child of this.element.children){
                if(child.nodeName.startsWith("animate")){
                    if(this.speed != 0 && !this.start){
                        this.start = true;
                        this.element.beginElement();
                    }

                    if(this.speed > 0){
                        child.setAttribute("dur", this.speed+"s");
                    }
                    else if(this.speed < 0){
                        child.setAttribute("dur", (-1)*this.speed+"s");
                    }
                    else{
                        this.start = false;
                        this.element.endElement();
                    }
                }
           }
        }

        init() {
            let widget_pos = this.element.getBBox();
            this.widget_center = [(widget_pos.x+widget_pos.width/2), (widget_pos.y+widget_pos.height/2)];
        }
    }
    ||
}


template "widget[@type='Animate']", mode="widget_defs" {
    param "hmi_element";
    |,
}