Skip to main content
Engineering LibreTexts

11.9: More about the Canvas

  • Page ID
    39213
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)

    The drawOn: method has an instance of Canvas as its sole argument; the canvas is the area on which the morph draws itself. By using the graphics methods of the canvas you are free to give the appearance you want to a morph. If you browse the inheritance hierarchy of the Canvas class, you will see that it has several variants. The default variant of Canvas is FormCanvas; you will find the key graphics methods in Canvas and FormCanvas. These methods can draw points, lines, polygons, rectangles, ellipses, text, and images with rotation and scaling.

    It is also possible to use other kinds of canvas, to obtain transparent morphs, more graphics methods, antialiasing, and so on. To use these features you will need an AlphaBlendingCanvas or a BalloonCanvas. But how can you obtain such a canvas in a drawOn: method, when drawOn: receives an instance of FormCanvas as its argument? Fortunately, you can transform one kind of canvas into another.

    \(\bigstar\) To use a canvas with a 0.5 alpha-transparency in DieMorph, redefine drawOn: like this:

    Code \(\PageIndex{1}\) (Squeak): Drawing a Translucent Die

    DieMorph»drawOn: aCanvas
        | theCanvas |
        theCanvas := aCanvas asAlphaBlendingCanvas: 0.5.
        super drawOn: theCanvas.
        (self perform: ('face' , dieValue asString) asSymbol)
            do: [:aPoint | self drawDotOn: theCanvas at: aPoint]
    

    That’s all you need to do!

    If you’re curious, have a look at the asAlphaBlendingCanvas: method. You can also get antialiasing by using BalloonCanvas and transforming the die drawing methods as shown in Code \(\PageIndex{2}\).

    The die displayed with alpha-transparency.
    Figure \(\PageIndex{1}\): The die displayed with alpha-transparency.

    Code \(\PageIndex{2}\) (Squeak): Drawing an Antialiased Die

    DieMorph»drawOn: aCanvas
        | theCanvas |
        theCanvas := aCanvas asBalloonCanvas aaLevel: 3.
        super drawOn: aCanvas.
        (self perform: ('face' , dieValue asString) asSymbol)
            do: [:aPoint | self drawDotOn: theCanvas at: aPoint]
    
    DieMorph»drawDotOn: aCanvas at: aPoint
        aCanvas
            drawOval: (Rectangle
                center: self position + (self extent * aPoint)
                extent: self extent / 6)
            color: Color black
            borderWidth: 0
            borderColor: Color transparent

    This page titled 11.9: More about the Canvas is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.