Skip to content

02 Canvas

Milton Mamani Torres edited this page Mar 10, 2020 · 5 revisions

Canvas

RSCanvas is the main class for roassal visualizations, it is a container of shapes and animations, and describes how this elements interact with the Pharo ecosystem, you can inspect it or open it in a system window, or create a morph and put it into a new window.

canvas := RSCanvas new.
canvas color: Color white.
window := canvas open.
window setLabel: 'Roassal canvas'.

the previous example uses open, this method open a new SystemWindow instance, and then we can change the window title.

Shapes in RSCanvas are separated in different groups, shapes, fixedShapes, nodes and edges.

Camera

RSCanvas has a reference to an instance of RSCamera, this object modifies the position and the scale of the group shapes, it has some methods to adjust the camera position and scale to focus to the elements.

canvas := RSCanvas new.
0 to: 10 do: [ :i | 
	canvas add: (RSBox new
		position: (i@i)* 20;
		color: (i even 
			ifTrue: [Color red ]
			ifFalse: [ Color blue ] );
		size: 20;
		yourself)  ].
canvas

In the previous example we put 10 red-blue boxes. The origin for these shapes is the center of the canvas. The first red box is the center of the visualization. But the visualization is not focus on the shapes, there are white areas that takes a lot of space. with the camera we can change the position to focus all the shapes.

...
canvas zoomToFit.

With zoomToFit canvas changes the position and scale of the camera without changing the position and scale of each shape.

Also just for fun we can change the viewing area of the camera to focus only on the first 3 first boxes.

canvas := RSCanvas new.
0 to: 10 do: [ :i | 
	canvas add: (RSBox new
		position: (i@i)* 20;
		color: (i even 
			ifTrue: [Color red ]
			ifFalse: [ Color blue ] );
		size: 20;
		yourself)  ].
group := canvas shapes copyFrom: 1 to: 3.
canvas open.
canvas camera 
	zoomToFit: canvas extent*0.8 
	rectangle: group encompassingRectangle.
canvas signalUpdate

In previous example there is a group with the first 3 shapes, then we open the window, this creates the canvas and sets its extent. With zoomToFil:rectangle: we change the position and the scale of the camera to fit in the 80% of the canvas extent, the rectangle that contains all the boxes of the group. This changes the canvas camera but it does not notify the change, thats why we need the signalUpdate. to refresh the canvas view.

Shapes by layer

RSCanvas has 2 big groups shapes and fixedShapes first canvas draws the group shapes then draws the group fixedShapes.

Shapes

Fixed shapes

nodes

edges

RSMorph

canvas events

Clone this wiki locally