Skip to main content
Inspiring
March 22, 2022
Question

HTML5 Canvas - Rotate symbol and convert angle between 1 and 180 degrees

  • March 22, 2022
  • 1 reply
  • 986 views

I am rotating a symbol in code.  During the rotation, the code converts to symbol's rotation property:

 

if (LFAngle > 180){
LFAngle = LFAngle - 180;
}
if(LFAngle == 0){
LFAngle = 180;
}

This conversion is a technical requirement.

 

When rotating the symbol in the compiled application,  there are some logic problems with the LFAngle:

 

LFAngle will show 0, and then 180 on another roation step.

 

The first rotation of 360 degrees is OK (except the issues above), but then starts increasing from 180 to 360, which is what the logic above is tying to stop.

 

Full code:

 

var LFAngle;

function updateLF_RotateCW() {
	var angle = 1 / Math.PI;
	root.LFLensHolder.rotation += angle;
	root.LFLensSupports.rotation += angle;
	root.lensParentLeft.rotation += angle;
	LFAngle = root.lensParentLeft.rotation;
	if (LFAngle > 180){
		LFAngle = LFAngle - 180;
	}
	if(LFAngle == 0){
		LFAngle = 180;
	} 
	root.cylinderAngle.text = (Math.round(((LFAngle) * 100) / 100))  + "\u00B0";
}

 

    This topic has been closed for replies.

    1 reply

    FlatChatAuthor
    Inspiring
    March 22, 2022

    I missed including the listener before the function:

     

    function LF_RotateCW_Down() {
    	createjs.Ticker.addEventListener("tick", updateLF_RotateCW);
    }
    
    function LF_RotateCW_Up() {
    	createjs.Ticker.removeEventListener("tick", updateLF_RotateCW);
    }
    
    var LFAngle;
    
    function updateLF_RotateCW() {
    	var angle = 1 / Math.PI;
    	root.LFLensHolder.rotation += angle;
    	root.LFLensSupports.rotation += angle;
    	root.lensParentLeft.rotation += angle;
    	LFAngle = root.lensParentLeft.rotation;
    	if (LFAngle > 180){
    		LFAngle = LFAngle - 180;
    	}
    	if(LFAngle == 0){
    		LFAngle = 180;
    	} 
    	root.cylinderAngle.text = (Math.round(((LFAngle) * 100) / 100))  + "\u00B0";

     

    See video below, which shows the object rotating in the bottom right, and the dynamic text field top left showing the rotation angle, which should be converted in the above function, to keep the angle between 1 and 180 degrees.

     

    Do I need a loop for the logic perhaps??  I would have thought that the ticker would act as a loop; is it resetting on the ticker??

     

     

    FlatChatAuthor
    Inspiring
    March 22, 2022

    Missed adding the closing brace on the above function to the snippet, but it exists...

     

    FlatChatAuthor
    Inspiring
    March 23, 2022

    The option to edit posts is under the More menu.

     

    Okay look, this is the important part: Every time your update function runs, you rotate your stage objects, then you set LFAngle's value equal to the rotation of one of your stage objects.

     

    Do you understand now why subtracting 180 from LFAngle isn't have a permanent effect?


    @ClayUUID   Yes, I can see that, but why does the logic work in the first rotation of root.LFLensHolder and not once that rotation is completed?   You can see that in the last video I posted.

     

    How should I do this to keep LFAngle between 1 and 180 as the object is rotated?

     

    Thanks for your help @ClayUUID