Images, pixels, canvas, tigers and bears…

A presentation at Programmed in Pencil in June 2020 in by Chris Heilmann

Slide 1

Slide 1

IMAGES, PIXELS, CANVAS, TIGERS AND BEARS…

Slide 2

Slide 2

DEVELOPER TOOLS EDGE(CHROMIUM) PM TEAM Zoher Ghadyali @ZGhadyali Chris Heilmann @codepo8 Erica Draud @hiamerica Rachel Weil @partytimeHXLNT Brendyn Alexander @webrendyn

Slide 3

Slide 3

N E R D, G E E K , F E A R L E S S C R E ATO R …

Slide 4

Slide 4

SOFTWARE IS A LW AY S PEEKING UNDER THE H O O D…

Slide 5

Slide 5

WHEN YOU SEE AN IMAGE, I SEE PIXELS AND D ATA F O R M AT S …

Slide 6

Slide 6

GETTING AN IMAGE INTO THE BROWSER… UPLOAD FORM DRAG AND DROP COPY AND PASTE https://christianheilmann.com/2020/03/20/fun-with-browsers-how-to-get-an-image-into-the-current-page/

Slide 7

Slide 7

THE HTML

Slide 8

Slide 8

FILE UPLOAD JAVASCRIPT

Slide 9

Slide 9

DRAG AND DROP JAVASCRIPT

Slide 10

Slide 10

COPY AND PASTE JAVASCRIPT

Slide 11

Slide 11

SHOWING THE IMAGE

Slide 12

Slide 12

THIS IS BORING

Slide 13

Slide 13

UNLOCKING T H E DATA IN THE IMAGE

Slide 14

Slide 14

Super basic in-browser painting mechanism Built for speed, not for comfort CANVAS Powerful, once you get it Confusing and weird till you get there Element: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/canvas API: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API

Slide 15

Slide 15

ETCH-A-SKETCH IN YOUR DOCUMENT

Slide 16

Slide 16

P E N U P, P E N DOWN…

Slide 17

Slide 17

• Variable fill, stroke, line end and pattern options INTERESTING CANVAS F E AT U R E S • Blend modes • Dynamic coordinate system – define coordinates once and rotate/zoom coordinate system • Drawing stack – you can store and save the state of the canvas • Everything results in an image

Slide 18

Slide 18

OPENING UP THE IMAGE

Slide 19

Slide 19

COPYING THE IMAGE TO THE CANVAS

Slide 20

Slide 20

Slide 21

Slide 21

T H E I M A G E D ATA OBJECT • width: Width of the canvas in pixels • height: Height of the canvas in pixels • data: Array of all the pixels in RGBA format

Slide 22

Slide 22

EXAMPLE: A N A LY S E AND COUNT USED COLOURS

Slide 23

Slide 23

GET IMAGE, ADD TO CANVAS, SEND PIXELS

Slide 24

Slide 24

LOOP OVER ALL PIXELS AND STORE THEIR VALUES IN AN OBJECT

Slide 25

Slide 25

SORT THE OBJECT AND PRINT OUT A LIST

Slide 26

Slide 26

USING I M A G E D ATA • You can read with getImageData and write with putImageData • In between everything is array manipulation • You can create pixel patterns • You can change each pixel of an image source. This allows for writing filters, removing backgrounds, recolouring, etc…

Slide 27

Slide 27

DON’T MAKE YOUR LIFE TOO HARD

Slide 28

Slide 28

DON’T MAKE YOUR LIFE TOO HARD

Slide 29

Slide 29

THINKING TOO COMPLEX • Get the mouse coordinates • Loop through the array and check if y times the width of the canvas was reached • Stop at the x index and read the colour.

Slide 30

Slide 30

OR…

Slide 31

Slide 31

CANVAS AND IMAGES • drawImage() is not only there to put an image onto the canvas • You can also crop and transform image with it • Scaling and cropping is easier • Rotation means you need to rotate the coordinate system around it

Slide 32

Slide 32

CROPPING NEEDS SOME GETTING USED TO…

Slide 33

Slide 33

ZOOMING A WHOLE IMAGE…

Slide 34

Slide 34

ZOOMING IS EASIER BY SCALING THE COORDINATE SYSTEM

Slide 35

Slide 35

INTERACTIVE ZOOMING

Slide 36

Slide 36

INTERACTIVE ZOOMING

Slide 37

Slide 37

INTERACTIVE ZOOMING USING CROP AND RESIZE

Slide 38

Slide 38

VALID IMAGE RESOURCES • Any image (must be on the same domain) • Any video (^ that) • Any other canvas

Slide 39

Slide 39

SAVING THE FINAL IMAGE • Right-click works • Other than that, you can do a canvas.toDataURL() • By default, canvas creates PNG files, but you can also do WebP or JPG • The latter takes a quality parameter

Slide 40

Slide 40

BONUS TRICK: ANCHOR AROUND CANVAS

Slide 41

Slide 41

SOME THINGS I H A V E B U I LT W I T H THESE TECHNIQUES

Slide 42

Slide 42

MAKETHUMBNAILS.COM DROP A BUNCH OF DEFINE THE SIZE THUMBNAILS, OR D E FAU LT IMAGES OF THE CHOOSE DOWNLOAD THE THUMBNAILS ONE BY ONE OR AS A ZIP ALL ON YOUR OWN MACHINE

Slide 43

Slide 43

R E M OV E P H OTO DATA . CO M

Slide 44

Slide 44

COLOUR PICKER FROM IMAGE https://christianheilmann.com/2020/04/22/limitinginput-type-color-to-a-certain-palette-from-an-image/

Slide 45

Slide 45

LO G O - O - M AT I C https://codepo8.github.io/logo-o-matic/

Slide 46

Slide 46

LO G O - O - M AT I C https://codepo8.github.io/logo-o-matic/

Slide 47

Slide 47

• Canvas doesn’t throw errors if you paint outside the available space, but it uses memory • If you only need to convert with a canvas, don’t even add it to the DOM BONUS TIPS: • If you do a lot of array looping and changes, use a worker to avoid the interface to slow down. Canvas, however isn’t available in a worker! • Resizing a canvas is a cheap way to clear it • In animations, using a rgba(r,g,b,0.5) value for clearing the canvas gives a smooth ghosting effect • A black and white map is a great way to do background collision in games – just read the next few pixels in direction of the move and if they are white – boom

Slide 48

Slide 48

THANK YOU AND HAVE FUN! Chris Heilmann Chris Heilmann - codepo8 @codepo8 @EdgeDevTools christianheilmann.com Click the feedback in Edge developer tools! 6/24/20 48