ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Canvas and Web Vector
                       Graphics
       Dylan Schiemann (@dylans)
       SitePen, Inc.
       HTML5 Code Camp, October, 2010


        ? SitePen, Inc. All Rights Reserved

Saturday, October 16, 2010
In the beginning, there was ASCII art
                   <img />
                   Microsoft and VML
                   Adobe, the W3C and SVG
                   Firefox and Opera get native SVG
                   Firefox, Opera and Safari get canvas
                   All non-IE browsers get canvas and SVG
                   IE9: 2011




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
1996




    Topology and Rheology of Quasi Two-Dimensional Foam
        http://arxiv.org/pdf/cond-mat/9904101


        ? SitePen, Inc. All Rights Reserved

Saturday, October 16, 2010
Raster vs. Vector

                   Raster: Rectangular grid of pixels
                          Pre-rendered before runtime (JPG, PNG)
                          Rendered at runtime (Canvas)
                   Vector: Mathematical representation of a shape
                          Rendered at runtime (SVG)




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
Native vs. Plug-in

                   Open Protocols
                          No proprietary player or studio required
                   Use seamlessly with HTML, CSS, DOM
                   No install needed
                   A modular piece of the web rather than trying to replace it




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
SVG vs. Canvas
                                                                SVG                     Canvas
                      Representation                             Pixels                 DOM Nodes
                            Scalability                       Vector                       Raster
                           Syntax/Size                         Verbose                     Terse
                      Event Handling                     DOM Events                     Pixel Coords
                   Browser Support                    IE9 beta, all majors             IE9?, all majors
                     Mobile Support                              Many                      More
                          Animations                 JavaScript/SMIL                       Timers
                         Accessibility                            Yes                        No
                           Image Save                             No                 Yes (PNG or JPG)

        http://dev.opera.com/articles/view/svg-or-canvas-choosing-between-the-two/


        ? SitePen, Inc. All Rights Reserved

Saturday, October 16, 2010
2D vs. 3D

                   2D
                          SVG, Canvas, etc.
                   3D
                          WebGL (FF, Chrome, Safari dev builds)
                                replaces O3D, Canvas 3D
                          SVG 3D Transforms




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
CSS 3 Extensions

                   Bringing the most important parts of SVG to HTML+CSS!
                          Gradients
                          Transforms (2D and 3D)
                          Transitions
                          Animations
                          Masks
                   Blurring the lines
                          Canvas as a background image
                          HTML elements inside SVG elements




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
Toolkits

                   Low-level (shapes, lines, events, etc.):
                          DojoX GFX (SVG, VML, Canvas, Silverlight, SVGWeb)
                          MooTools ART (SVG, VML)
                          Processing (Canvas)
                          Rapha?l (SVG, VML)
                   High-level
                          DojoX Charting, Drawing
                          MooTools ART Widgets
                          PlotKit and many other charting projects




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
Toolkits

                   Low-level (shapes, lines, events, etc.):
                          Dojo GFX, MooTools ART, Processing, Rapha?l
                   High-level
                          DojoX Charting, Drawing
                          MooTools ART Widgets
                          PlotKit and many other charting projects




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
Let's Draw Something




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
Canvas Code - Basic Structure

          <!DOCTYPE html>
          <html lang="en">
          <head>
              <title>Canvas Example</title>
          </head>
          <body>

              <!-- canvas element; container -->
              <canvas id="myCanvas" width="320" height="320">
          Your browser does not have support for Canvas.
              </canvas>

          </body>
          </html>




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
Canvas Example: London Ajax Logo

                <script>
                     // Set up a few variables, including our canvas and context
                     var canvas = document.getElementById('canvas'),
                         ctx = canvas.getContext('2d'),
                         grad;

                           // Build the path for the V
                           ctx.beginPath();
                           ctx.moveTo(7, 105);
                           ctx.lineTo(25, 105);
                           ctx.lineTo(60, 31);
                           ctx.lineTo(96, 105);
                           ctx.lineTo(114, 105);
                           ctx.lineTo(69, 11);
                           ctx.lineTo(52, 11);
                           ctx.closePath();

                           // Set up the stroke
                           ctx.strokeStyle = '#a70017';
                           ctx.stroke();

                           // Set up the gradient
                           grad = ctx.createLinearGradient(0, 0, 0, 105);
                           grad.addColorStop(0, '#f3001f');
                           grad.addColorStop(1, '#a40016');


    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
Canvas Example: London Ajax Logo (cont)

                           // ... code continued from previous slide ...

                           // Apply the gradient to the V
                           ctx.fillStyle = grad;
                           ctx.fill();

                           // Create the blue box
                           ctx.fillStyle = '#0000ae';
                           ctx.fillRect(8, 68, 103, 16);

                      // Create the text
                      ctx.font = 'bold 9pt Arial';
                      ctx.fillStyle = '#ffffff';
                      ctx.fillText('LONDON AJAX', 16, 80);
                      ctx.strokeStyle = '#ffffff';
                      ctx.strokeText('LONDON AJAX', 16, 80);
                  </script>




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
SVG Code - Basic Structure

          <?xml version="1.0" standalone="no"?>
          <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/
          DTD/svg11.dtd">

          <!-- containing node -->
          <svg width=?"480" height=?"480">?

              <!-- defs: contains "referenced" elements -->
              <defs>?

              </defs>?

              <!-- add all shapes here -->

          </svg>




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010

I think of ¡°defs¡± as almost an array of vars, indexed by an ID attribute
SVG Example: London Ajax Logo

          <?xml version="1.0" standalone="no"?>
          <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/
          DTD/svg11.dtd">

          <!-- container -->
          <svg width="480" height="480" xmlns="http://www.w3.org/2000/svg" version="1.1">?

              <!-- definitions -->
              <defs>?

              <!-- linear gradient for the "A" -->
              <!-- referenced as "Gradient1" -->
              <linearGradient id="Gradient1" gradientUnits="userSpaceOnUse" x1="0.00000000"
          y1="0.00000000" x2="0.00000000" y2="420.00000000">?
                 <stop offset="0.00000000" stop-color="rgb(243, 0, 31)" stop-opacity="1">?</
          stop>?
                 <stop offset="1.00000000" stop-color="rgb(164, 0, 22)" stop-opacity="1">?</
          stop>?
              </linearGradient>?

              </defs>?

              <!-- shapes on next slide -->




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
SVG Example: London Ajax Logo (cont.)

              <!-- polyline to create the upside down "V" shape -->
              <!-- uses gradation fill -->
              <polyline fill="url(/slideshow/html5-codecampcanvasvectors/5460205/)" style="fill:url(/slideshow/html5-codecampcanvasvectors/5460205/);" stroke="rgb(167,
          0, 23)" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke-
          linejoin="miter" stroke-miterlimit="4" points="28 420 100 420 240 124 384 420 456
          420 276 44 208 44 28 420" stroke-dasharray="none" fill-rule="evenodd" />

              <!-- blue rect shape to complete the "A" -->
              <rect fill="rgb(0, 0, 174)" fill-opacity="1" stroke="none" stroke-opacity="0"
          stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-
          miterlimit="4" x="32" y="272" width="412" height="64" fill-rule="evenodd" />?

              <!-- text for "LONDON AJAX" -->
              <text fill="rgb(255, 255, 255)" fill-opacity="1" stroke="none" stroke-
          opacity="0" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke-
          miterlimit="4" x="64" y="320" text-anchor="start" text-decoration="none" rotate="0"
          kerning="auto" text-rendering="optimizeLegibility" font-style="normal" font-
          variant="normal" font-weight="bold" font-size="36pt" font-family="Arial" fill-
          rule="evenodd">?LONDON AJAX?</text>?

          </svg>




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010

https://user.sitepen.com/~dwalsh/ajax-london.svg
Two Ways to Include SVG

                   <object>
                   <iframe>




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
GFX Example: London Ajax Logo

          <!-- node which will contain the drawing -->

          <script>
              // Require gfx
              dojo.require('dojox.gfx');
              dojo.require('dojox.gfx.gradient');

                  // When the resources have loaded....
                  dojo.ready(function() {

                           // Grab the DIV that will contain the drawing
                           var refNode = dojo.byId('someNode');

                           // Create the GFX "surface"
                           var surface = new dojox.gfx.createSurface(refNode,120,120);




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
GFX Example: London Ajax Logo (cont)

                  // Create an upside-down "V"
                  surface.createPolyline([
                      { x:7, y:105 },
                      { x:25, y:105 },
                      { x:60, y:31 },
                      { x:96, y:105 },
                      { x:114, y:105 },
                      { x:69, y:11 },
                      { x:52, y:11 },
                      { x:7, y:105 }
                  ]).
          setStroke({color:'#a70017'}).
          setFill({ type:'linear', x1:0, y1:0, x2:0, y2:105, colors: [{ offset:0,
          color:'#f3001f'},{ offset:1, color:'#a40016'}] });

                           // Create the blue box
                           surface.createRect({ x:8, y:68, width:103, height:16 }).
                                    setFill('#0000ae');

                           // Create the text
                           surface.createText({ x:16, y:80, text:'LONDON AJAX', align:'start'}).
                                    setFont({ family:'Arial', size:'9pt', weight:'bold' }).
                                    setFill('#ffffff');

              });
          </script>

    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
Example

                   Fun with the London Ajax logo




    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
Canvas Compatibility

          <!DOCTYPE html>
          <html lang="en">
          <head>
              <title>Canvas Example</title>
          </head>
          <body>
           <canvas id="myCanvas" width="320" height="320">
             Your browser does not have support for Canvas.
           </canvas>
           <script>
                  var canvas = document.getElementById('myCanvas'),
                       ctx = canvas.getContext('2d'),
                       grad;
          " " ctx.fillRect(100, 25, 100, 50);

                  ctx.beginPath();
          " " // if you forget false in FF ff 3.6 or 4 beta, it won't draw?!?
                  ctx.arc(150, 150, 100, Math.PI * 3/2, Math.PI * 1/2, false);
                  ctx.lineWidth = 2;
                  ctx.lineCap = 'round';
                  ctx.strokeStyle = 'rgba(0, 127, 255, 0.3)';
                  ctx.stroke();
           </script>
          </body>
          </html>


    ? SitePen, Inc. All Rights Reserved
Saturday, October 16, 2010
? SitePen, Inc. All Rights Reserved

Saturday, October 16, 2010
Ad

Recommended

Comet web applications with Python, Django & Orbited
Comet web applications with Python, Django & Orbited
PyCon Italia
?
HTML5: Toolkits and Gaps
HTML5: Toolkits and Gaps
dylanks
?
Intro to WebSockets and Comet
Intro to WebSockets and Comet
dylanks
?
Dojo Mobile
Dojo Mobile
dylanks
?
Advanced AV Foundation (CocoaConf, Aug '11)
Advanced AV Foundation (CocoaConf, Aug '11)
Chris Adamson
?
Groovy, to Infinity and Beyond - Groovy/Grails eXchange 2009
Groovy, to Infinity and Beyond - Groovy/Grails eXchange 2009
Guillaume Laforge
?
GWT Overview And Feature Preview - SV Web JUG - June 16 2009
GWT Overview And Feature Preview - SV Web JUG - June 16 2009
Fred Sauer
?
Building complex and modular RIAs with OSGi and Flex
Building complex and modular RIAs with OSGi and Flex
CARA_Lyon
?
Web Vector Graphics
Web Vector Graphics
dylanks
?
Svghtml5 Meetup
Svghtml5 Meetup
Oswald Campesato
?
HTML5 - A Whirlwind tour
HTML5 - A Whirlwind tour
Lohith Goudagere Nagaraj
?
Basic html5 and javascript
Basic html5 and javascript
wendy017
?
Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?
Patrick Chanezon
?
HTML5 Intoduction for Web Developers
HTML5 Intoduction for Web Developers
Sascha Corti
?
Familiar HTML5 - ÊÂÀý¤È¥µ¥ó¥×¥ë¥³©`¥É¤«¤éѧ¤Ö Éí½ü¤ÇÆÕͨ¤Ëʹ¤ï¤Æ¤¤¤ëHTML5
Familiar HTML5 - ÊÂÀý¤È¥µ¥ó¥×¥ë¥³©`¥É¤«¤éѧ¤Ö Éí½ü¤ÇÆÕͨ¤Ëʹ¤ï¤Æ¤¤¤ëHTML5
Sadaaki HIRAI
?
HTML5 Intro
HTML5 Intro
PavingWays Ltd.
?
Speak the Web 15.02.2010
Speak the Web 15.02.2010
Patrick Lauke
?
How to build a html5 websites.v1
How to build a html5 websites.v1
Bitla Software
?
soft-shake.ch - Introduction to HTML5
soft-shake.ch - Introduction to HTML5
soft-shake.ch
?
Svcc 2013-css3-and-mobile
Svcc 2013-css3-and-mobile
Oswald Campesato
?
Bruce Lawson, Web Development 2.0, SparkUp! Poznan Poland
Bruce Lawson, Web Development 2.0, SparkUp! Poznan Poland
brucelawson
?
Cape Cod Web Technology Meetup - 2
Cape Cod Web Technology Meetup - 2
Asher Martin
?
Word camp nextweb
Word camp nextweb
GreekTuts ¦¥¦Ë¦Ë¦Ç¦Í¦É¦Ê? ¦¢¦Ï¦Ç¦È?¦Ì¦Á¦Ó¦Á
?
Word camp nextweb
Word camp nextweb
Panagiotis Grigoropoulos
?
WordCamp Thessaloniki2011 The NextWeb
WordCamp Thessaloniki2011 The NextWeb
George Kanellopoulos
?
JavaONE 2012 Using Java with HTML5 and CSS3
JavaONE 2012 Using Java with HTML5 and CSS3
Helder da Rocha
?
Academy PRO: HTML5 API graphics
Academy PRO: HTML5 API graphics
Binary Studio
?
DevChatt: The Wonderful World Of Html5
DevChatt: The Wonderful World Of Html5
Cameron Kilgore
?
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) ºÝºÝߣs
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) ºÝºÝߣs
Ravi Tamada
?
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
?

More Related Content

Similar to Introduction to Canvas and Native Web Vector Graphics (20)

Web Vector Graphics
Web Vector Graphics
dylanks
?
Svghtml5 Meetup
Svghtml5 Meetup
Oswald Campesato
?
HTML5 - A Whirlwind tour
HTML5 - A Whirlwind tour
Lohith Goudagere Nagaraj
?
Basic html5 and javascript
Basic html5 and javascript
wendy017
?
Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?
Patrick Chanezon
?
HTML5 Intoduction for Web Developers
HTML5 Intoduction for Web Developers
Sascha Corti
?
Familiar HTML5 - ÊÂÀý¤È¥µ¥ó¥×¥ë¥³©`¥É¤«¤éѧ¤Ö Éí½ü¤ÇÆÕͨ¤Ëʹ¤ï¤Æ¤¤¤ëHTML5
Familiar HTML5 - ÊÂÀý¤È¥µ¥ó¥×¥ë¥³©`¥É¤«¤éѧ¤Ö Éí½ü¤ÇÆÕͨ¤Ëʹ¤ï¤Æ¤¤¤ëHTML5
Sadaaki HIRAI
?
HTML5 Intro
HTML5 Intro
PavingWays Ltd.
?
Speak the Web 15.02.2010
Speak the Web 15.02.2010
Patrick Lauke
?
How to build a html5 websites.v1
How to build a html5 websites.v1
Bitla Software
?
soft-shake.ch - Introduction to HTML5
soft-shake.ch - Introduction to HTML5
soft-shake.ch
?
Svcc 2013-css3-and-mobile
Svcc 2013-css3-and-mobile
Oswald Campesato
?
Bruce Lawson, Web Development 2.0, SparkUp! Poznan Poland
Bruce Lawson, Web Development 2.0, SparkUp! Poznan Poland
brucelawson
?
Cape Cod Web Technology Meetup - 2
Cape Cod Web Technology Meetup - 2
Asher Martin
?
Word camp nextweb
Word camp nextweb
GreekTuts ¦¥¦Ë¦Ë¦Ç¦Í¦É¦Ê? ¦¢¦Ï¦Ç¦È?¦Ì¦Á¦Ó¦Á
?
Word camp nextweb
Word camp nextweb
Panagiotis Grigoropoulos
?
WordCamp Thessaloniki2011 The NextWeb
WordCamp Thessaloniki2011 The NextWeb
George Kanellopoulos
?
JavaONE 2012 Using Java with HTML5 and CSS3
JavaONE 2012 Using Java with HTML5 and CSS3
Helder da Rocha
?
Academy PRO: HTML5 API graphics
Academy PRO: HTML5 API graphics
Binary Studio
?
DevChatt: The Wonderful World Of Html5
DevChatt: The Wonderful World Of Html5
Cameron Kilgore
?
Web Vector Graphics
Web Vector Graphics
dylanks
?
Basic html5 and javascript
Basic html5 and javascript
wendy017
?
Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?
Patrick Chanezon
?
HTML5 Intoduction for Web Developers
HTML5 Intoduction for Web Developers
Sascha Corti
?
Familiar HTML5 - ÊÂÀý¤È¥µ¥ó¥×¥ë¥³©`¥É¤«¤éѧ¤Ö Éí½ü¤ÇÆÕͨ¤Ëʹ¤ï¤Æ¤¤¤ëHTML5
Familiar HTML5 - ÊÂÀý¤È¥µ¥ó¥×¥ë¥³©`¥É¤«¤éѧ¤Ö Éí½ü¤ÇÆÕͨ¤Ëʹ¤ï¤Æ¤¤¤ëHTML5
Sadaaki HIRAI
?
Speak the Web 15.02.2010
Speak the Web 15.02.2010
Patrick Lauke
?
How to build a html5 websites.v1
How to build a html5 websites.v1
Bitla Software
?
soft-shake.ch - Introduction to HTML5
soft-shake.ch - Introduction to HTML5
soft-shake.ch
?
Bruce Lawson, Web Development 2.0, SparkUp! Poznan Poland
Bruce Lawson, Web Development 2.0, SparkUp! Poznan Poland
brucelawson
?
Cape Cod Web Technology Meetup - 2
Cape Cod Web Technology Meetup - 2
Asher Martin
?
WordCamp Thessaloniki2011 The NextWeb
WordCamp Thessaloniki2011 The NextWeb
George Kanellopoulos
?
JavaONE 2012 Using Java with HTML5 and CSS3
JavaONE 2012 Using Java with HTML5 and CSS3
Helder da Rocha
?
Academy PRO: HTML5 API graphics
Academy PRO: HTML5 API graphics
Binary Studio
?
DevChatt: The Wonderful World Of Html5
DevChatt: The Wonderful World Of Html5
Cameron Kilgore
?

Recently uploaded (20)

Hyderabad MuleSoft In-Person Meetup (June 21, 2025) ºÝºÝߣs
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) ºÝºÝߣs
Ravi Tamada
?
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
?
Wenn alles versagt - IBM Tape sch¨¹tzt, was z?hlt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape sch¨¹tzt, was z?hlt! Und besonders mit dem neust...
Josef Weingand
?
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
All Things Open
?
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
?
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
?
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
?
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
?
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
?
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
?
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
?
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
?
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
?
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
?
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
?
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
?
The Growing Value and Application of FME & GenAI
The Growing Value and Application of FME & GenAI
Safe Software
?
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
?
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
?
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
?
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) ºÝºÝߣs
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) ºÝºÝߣs
Ravi Tamada
?
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
AI Agents and FME: A How-to Guide on Generating Synthetic Metadata
Safe Software
?
Wenn alles versagt - IBM Tape sch¨¹tzt, was z?hlt! Und besonders mit dem neust...
Wenn alles versagt - IBM Tape sch¨¹tzt, was z?hlt! Und besonders mit dem neust...
Josef Weingand
?
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
Agentic AI for Developers and Data Scientists Build an AI Agent in 10 Lines o...
All Things Open
?
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
?
Cyber Defense Matrix Workshop - RSA Conference
Cyber Defense Matrix Workshop - RSA Conference
Priyanka Aash
?
Quantum AI: Where Impossible Becomes Probable
Quantum AI: Where Impossible Becomes Probable
Saikat Basu
?
2025_06_18 - OpenMetadata Community Meeting.pdf
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
?
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
?
Securing AI - There Is No Try, Only Do!.pdf
Securing AI - There Is No Try, Only Do!.pdf
Priyanka Aash
?
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
?
Connecting Data and Intelligence: The Role of FME in Machine Learning
Connecting Data and Intelligence: The Role of FME in Machine Learning
Safe Software
?
10 Key Challenges for AI within the EU Data Protection Framework.pdf
10 Key Challenges for AI within the EU Data Protection Framework.pdf
Priyanka Aash
?
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
?
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
?
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
WebdriverIO & JavaScript: The Perfect Duo for Web Automation
digitaljignect
?
The Growing Value and Application of FME & GenAI
The Growing Value and Application of FME & GenAI
Safe Software
?
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
?
"Scaling in space and time with Temporal", Andriy Lupa.pdf
"Scaling in space and time with Temporal", Andriy Lupa.pdf
Fwdays
?
Ad

Introduction to Canvas and Native Web Vector Graphics

  • 1. Canvas and Web Vector Graphics Dylan Schiemann (@dylans) SitePen, Inc. HTML5 Code Camp, October, 2010 ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 2. In the beginning, there was ASCII art <img /> Microsoft and VML Adobe, the W3C and SVG Firefox and Opera get native SVG Firefox, Opera and Safari get canvas All non-IE browsers get canvas and SVG IE9: 2011 ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 3. 1996 Topology and Rheology of Quasi Two-Dimensional Foam http://arxiv.org/pdf/cond-mat/9904101 ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 4. Raster vs. Vector Raster: Rectangular grid of pixels Pre-rendered before runtime (JPG, PNG) Rendered at runtime (Canvas) Vector: Mathematical representation of a shape Rendered at runtime (SVG) ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 5. Native vs. Plug-in Open Protocols No proprietary player or studio required Use seamlessly with HTML, CSS, DOM No install needed A modular piece of the web rather than trying to replace it ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 6. SVG vs. Canvas SVG Canvas Representation Pixels DOM Nodes Scalability Vector Raster Syntax/Size Verbose Terse Event Handling DOM Events Pixel Coords Browser Support IE9 beta, all majors IE9?, all majors Mobile Support Many More Animations JavaScript/SMIL Timers Accessibility Yes No Image Save No Yes (PNG or JPG) http://dev.opera.com/articles/view/svg-or-canvas-choosing-between-the-two/ ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 7. 2D vs. 3D 2D SVG, Canvas, etc. 3D WebGL (FF, Chrome, Safari dev builds) replaces O3D, Canvas 3D SVG 3D Transforms ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 8. CSS 3 Extensions Bringing the most important parts of SVG to HTML+CSS! Gradients Transforms (2D and 3D) Transitions Animations Masks Blurring the lines Canvas as a background image HTML elements inside SVG elements ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 9. Toolkits Low-level (shapes, lines, events, etc.): DojoX GFX (SVG, VML, Canvas, Silverlight, SVGWeb) MooTools ART (SVG, VML) Processing (Canvas) Rapha?l (SVG, VML) High-level DojoX Charting, Drawing MooTools ART Widgets PlotKit and many other charting projects ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 10. Toolkits Low-level (shapes, lines, events, etc.): Dojo GFX, MooTools ART, Processing, Rapha?l High-level DojoX Charting, Drawing MooTools ART Widgets PlotKit and many other charting projects ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 11. Let's Draw Something ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 12. Canvas Code - Basic Structure <!DOCTYPE html> <html lang="en"> <head> <title>Canvas Example</title> </head> <body> <!-- canvas element; container --> <canvas id="myCanvas" width="320" height="320"> Your browser does not have support for Canvas. </canvas> </body> </html> ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 13. Canvas Example: London Ajax Logo <script> // Set up a few variables, including our canvas and context var canvas = document.getElementById('canvas'), ctx = canvas.getContext('2d'), grad; // Build the path for the V ctx.beginPath(); ctx.moveTo(7, 105); ctx.lineTo(25, 105); ctx.lineTo(60, 31); ctx.lineTo(96, 105); ctx.lineTo(114, 105); ctx.lineTo(69, 11); ctx.lineTo(52, 11); ctx.closePath(); // Set up the stroke ctx.strokeStyle = '#a70017'; ctx.stroke(); // Set up the gradient grad = ctx.createLinearGradient(0, 0, 0, 105); grad.addColorStop(0, '#f3001f'); grad.addColorStop(1, '#a40016'); ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 14. Canvas Example: London Ajax Logo (cont) // ... code continued from previous slide ... // Apply the gradient to the V ctx.fillStyle = grad; ctx.fill(); // Create the blue box ctx.fillStyle = '#0000ae'; ctx.fillRect(8, 68, 103, 16); // Create the text ctx.font = 'bold 9pt Arial'; ctx.fillStyle = '#ffffff'; ctx.fillText('LONDON AJAX', 16, 80); ctx.strokeStyle = '#ffffff'; ctx.strokeText('LONDON AJAX', 16, 80); </script> ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 15. SVG Code - Basic Structure <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/ DTD/svg11.dtd"> <!-- containing node --> <svg width=?"480" height=?"480">? <!-- defs: contains "referenced" elements --> <defs>? </defs>? <!-- add all shapes here --> </svg> ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010 I think of ¡°defs¡± as almost an array of vars, indexed by an ID attribute
  • 16. SVG Example: London Ajax Logo <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/ DTD/svg11.dtd"> <!-- container --> <svg width="480" height="480" xmlns="http://www.w3.org/2000/svg" version="1.1">? <!-- definitions --> <defs>? <!-- linear gradient for the "A" --> <!-- referenced as "Gradient1" --> <linearGradient id="Gradient1" gradientUnits="userSpaceOnUse" x1="0.00000000" y1="0.00000000" x2="0.00000000" y2="420.00000000">? <stop offset="0.00000000" stop-color="rgb(243, 0, 31)" stop-opacity="1">?</ stop>? <stop offset="1.00000000" stop-color="rgb(164, 0, 22)" stop-opacity="1">?</ stop>? </linearGradient>? </defs>? <!-- shapes on next slide --> ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 17. SVG Example: London Ajax Logo (cont.) <!-- polyline to create the upside down "V" shape --> <!-- uses gradation fill --> <polyline fill="url(/slideshow/html5-codecampcanvasvectors/5460205/)" style="fill:url(/slideshow/html5-codecampcanvasvectors/5460205/);" stroke="rgb(167, 0, 23)" stroke-opacity="1" stroke-width="1" stroke-linecap="butt" stroke- linejoin="miter" stroke-miterlimit="4" points="28 420 100 420 240 124 384 420 456 420 276 44 208 44 28 420" stroke-dasharray="none" fill-rule="evenodd" /> <!-- blue rect shape to complete the "A" --> <rect fill="rgb(0, 0, 174)" fill-opacity="1" stroke="none" stroke-opacity="0" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke- miterlimit="4" x="32" y="272" width="412" height="64" fill-rule="evenodd" />? <!-- text for "LONDON AJAX" --> <text fill="rgb(255, 255, 255)" fill-opacity="1" stroke="none" stroke- opacity="0" stroke-width="1" stroke-linecap="butt" stroke-linejoin="miter" stroke- miterlimit="4" x="64" y="320" text-anchor="start" text-decoration="none" rotate="0" kerning="auto" text-rendering="optimizeLegibility" font-style="normal" font- variant="normal" font-weight="bold" font-size="36pt" font-family="Arial" fill- rule="evenodd">?LONDON AJAX?</text>? </svg> ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010 https://user.sitepen.com/~dwalsh/ajax-london.svg
  • 18. Two Ways to Include SVG <object> <iframe> ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 19. GFX Example: London Ajax Logo <!-- node which will contain the drawing --> <script> // Require gfx dojo.require('dojox.gfx'); dojo.require('dojox.gfx.gradient'); // When the resources have loaded.... dojo.ready(function() { // Grab the DIV that will contain the drawing var refNode = dojo.byId('someNode'); // Create the GFX "surface" var surface = new dojox.gfx.createSurface(refNode,120,120); ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 20. GFX Example: London Ajax Logo (cont) // Create an upside-down "V" surface.createPolyline([ { x:7, y:105 }, { x:25, y:105 }, { x:60, y:31 }, { x:96, y:105 }, { x:114, y:105 }, { x:69, y:11 }, { x:52, y:11 }, { x:7, y:105 } ]). setStroke({color:'#a70017'}). setFill({ type:'linear', x1:0, y1:0, x2:0, y2:105, colors: [{ offset:0, color:'#f3001f'},{ offset:1, color:'#a40016'}] }); // Create the blue box surface.createRect({ x:8, y:68, width:103, height:16 }). setFill('#0000ae'); // Create the text surface.createText({ x:16, y:80, text:'LONDON AJAX', align:'start'}). setFont({ family:'Arial', size:'9pt', weight:'bold' }). setFill('#ffffff'); }); </script> ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 21. Example Fun with the London Ajax logo ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 22. Canvas Compatibility <!DOCTYPE html> <html lang="en"> <head> <title>Canvas Example</title> </head> <body> <canvas id="myCanvas" width="320" height="320"> Your browser does not have support for Canvas. </canvas> <script> var canvas = document.getElementById('myCanvas'), ctx = canvas.getContext('2d'), grad; " " ctx.fillRect(100, 25, 100, 50); ctx.beginPath(); " " // if you forget false in FF ff 3.6 or 4 beta, it won't draw?!? ctx.arc(150, 150, 100, Math.PI * 3/2, Math.PI * 1/2, false); ctx.lineWidth = 2; ctx.lineCap = 'round'; ctx.strokeStyle = 'rgba(0, 127, 255, 0.3)'; ctx.stroke(); </script> </body> </html> ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010
  • 23. ? SitePen, Inc. All Rights Reserved Saturday, October 16, 2010