32
SVG SCALABLE VECTOR GRAPHICS Visualization on the Web

Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

SVGSCALABLEVECTORGRAPHICS

VisualizationontheWeb

Page 2: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

IntroducingSVG

§  Descriptivetagsforimages§  Basedonvectorgraphics§  D3.jscanmanagethecreationandmodificationoftags

Page 3: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

SVGExternalResources

§  SVGSpecification§  http://www.w3.org/TR/SVG/

§ MozillaDeveloperNetwork§  https://developer.mozilla.org/en/SVG

§  D3.jsAPIReference§  https://github.com/mbostock/d3/wiki/SVG-Shapes

Page 4: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

ObservableHQNotebookavailable

§  https://observablehq.com/collection/@rinziv/va602aa

Page 5: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

HelloWorldExample

<!DOCTYPE html> <meta charset="utf-8"> […] <svg width="960" height="500"> <circle cx="480" cy="250"> </circle> </svg> […]

Page 6: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

CoordinateSystem

Page 7: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

SVG–BASICSHAPES

Page 8: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Rectangle

<svg><rectwidth="200"height="100"fill="#BBC42A"/></svg>

http://codepen.io/jonitrythall/pen/525df2422b0ebc54c71a48d27534ea5e

Page 9: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Circle

<svg><circlecx="75"cy="75"r="75"fill="#ED6E46"/></svg>

http://codepen.io/jonitrythall/pen/088bbada7eed6739d09715666b945141

Page 10: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Ellipse

<svg><ellipsecx="100"cy="100"rx="100"ry="50"fill="#7AA20D"/></svg>

http://codepen.io/jonitrythall/pen/8ec26dac6d5b64bc663c03f01c5d60e0

Page 11: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Line

<svg><linex1="5"y1="5"x2="100"y2="100"stroke="#765373"stroke-width="8"/></svg>

Page 12: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Polyline

<svg><polylinepoints="0,4040,4040,8080,8080,120120,120120,160"fill="white"stroke="#BBC42A"stroke-width="6"/></svg>

Page 13: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Polygon

<svg><polygonpoints="50,5100,5125,30125,80100,10550,10525,8025,30"fill="#ED6E46"/></svg>

Page 14: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Path

<svgwidth="258px"height="184px"><pathfill="#7AA20D"stroke="#7AA20D"stroke-width="9"stroke-linejoin="round"d="M248.761,92c0,9.801-7.93,17.731-17.71,17.731c-0.319,0-0.617,0-0.935-0.021c-10.035,37.291-51.174,65.206-100.414,65.206c-49.261,0-90.443-27.979-100.435-65.334c-0.765,0.106-1.531,0.149-2.317,0.149c-9.78,0-17.71-7.93-17.71-17.731c0-9.78,7.93-17.71,17.71-17.71c0.787,0,1.552,0.042,2.317,0.149C39.238,37.084,80.419,9.083,129.702,9.083c49.24,0,90.379,27.937,100.414,65.228h0.021c0.298-0.021,0.617-0.021,0.914-0.021C240.831,74.29,248.761,82.22,248.761,92z"/></svg>

Page 15: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Pathspecifications

§  Definitionofapathisdonewithinapathelement§  <pathd=“{specificsofthepath}”/>

§  Thespecificsofpathareinstructionstomoveavirtualpenoverthegraphics§  Moveto(Morm).Gotocoordinatesliftingthepen,withoutatrace

§  Lineto(Lorl).Drawalinefromthelastpointtothenewcoordinates

§  VerticalorHorizontallines(Horh,Vorv).Drawalineparalleltooneoftheaxis

§  Closepath(Zorz)

Page 16: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Pathcommands–Uppercasevslowercasecommands

§  Anuppercaseletterindicatesabsolutecoordinateswillfollow

§  Alowercaseletterindicatesarelativecoordinate

Page 17: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Path–CubicBezier

<svg><pathfill="none"stroke="#333333"stroke-width="3"d="M10,55C10,5100,5100,55"/></svg>

Page 18: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Path–QuadraticbezierCurve

<svg><pathfill="none"stroke="#333333"stroke-width="3"d="M20,50Q40,5100,50"/></svg>

Page 19: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Example-Stairwayswithpath

<!--Stairwaysexampleusingpath--><svgwidth="200"height="200"><pathd="M0,40L40,40L40,80L80,80L80,120L120,120L120,160"fill="white"stroke="#BBC42A"stroke-width="6"/></svg>

Page 20: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Example-Stairwayswithpath

<!--StairwaysexampleusingpathwithHandVcommands--><svgwidth="200"height="200"><pathd="M0,40H40V80H80V120H120V160"fill="white"stroke="#BBC42A"stroke-width="6"/></svg>

Page 21: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Example-Stairwayswithpath

<!--Stairwaysexampleusingpathwithrelativecoordinates(handv)--><svgwidth="200"height="200"><pathd="M0,40h40v40h40v40h40v40"fill="white"stroke="#BBC42A"stroke-width="6"/></svg>

Page 22: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Stackingordering

<svg><gclass="grapes"><!--<path<stempath>/>--><!--<path<grapespath>/>--><!--<path<leafpath>/>--></g><gclass="watermelon"><!--<path<outsidepath>/>--><!--<path<insidepath>/>--><!--<path<seedspath>/>--></g></svg>

Page 23: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

CoordinateSystemTransform

transform="translate(<tx>,<ty>)rotate(<rotationangle>)

Page 24: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Transformations

§  Translate§  transform="translate(<tx>,<ty>)"

§  Rotate§  transform="rotate(<rotationangle>)"§  transform=rotate(<rotationangle>[<cx>,<cy>])"

§  Scale§  transform="scale(<sx>[<sy>])"

§  Skew§  transform="skewX(20)"

Page 25: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Circleexamplewithtranslation

<!--Drawacircleinthecenteroftheelementusingrelativecoordinatesafteratranslation--><svgwidth="200"height="100"><gtransform="translate(100,50)"><circler="50"/><circler="20"style="fill:#fdbb84"/></g></svg>

§  Liveexampleat:§  http://jsbin.com/kiwukat/2/edit?html,output

Page 26: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

PocketGuidetoWritingSVG

http://svgpocketguide.com/book/

Page 27: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

CANVASELEMENT

Page 28: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Canvas

§  Acanvaselementisacontainerforrastergraphics

§ Withinthecanvas,acontextprovidethefunctionstodrawvisualelements

§  Twodifferentcontexttypes:§  “2d”§  “webgl”

Page 29: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Canvas-Example

<p>Before canvas.</p> <canvas width="120" height="60"></canvas> <p>After canvas.</p> <script> var canvas = document.querySelector("canvas"); var context = canvas.getContext("2d"); context.fillStyle = "red"; context.fillRect(10, 10, 100, 50); </script>

Page 30: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Canvas-Path

<canvas></canvas><script>varcx=document.querySelector("canvas").getContext("2d");cx.beginPath();for(vary=10;y<100;y+=10){cx.moveTo(10,y);cx.lineTo(90,y);}cx.stroke();</script>

Page 31: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Canvas-Curves

<canvas></canvas><script>varcx=document.querySelector("canvas").getContext("2d");cx.beginPath();cx.moveTo(10,90);//control=(60,10)goal=(90,90)cx.quadraticCurveTo(60,10,90,90);cx.lineTo(60,10);cx.closePath();cx.stroke();</script>

Page 32: Visualization on the Web SVG SCALABLE VECTOR GRAPHICSdidawiki.cli.di.unipi.it/.../2020/va_lesson7_svg.pdf · Introducing SVG § Descriptive tags for images § Based on vector graphics

Canvas-Curves

<canvas></canvas><script>varcx=document.querySelector("canvas").getContext("2d");cx.beginPath();cx.moveTo(10,90);//control1=(10,10)control2=(90,10)goal=(50,90)cx.bezierCurveTo(10,10,90,10,50,90);cx.lineTo(90,10);cx.lineTo(10,10);cx.closePath();cx.stroke();</script>