65
HTML5 API graphics binary-studio.com

Academy PRO: HTML5 API graphics

Embed Size (px)

Citation preview

Page 1: Academy PRO: HTML5 API graphics

HTML5 APIgraphics

binary-studio.com

Page 2: Academy PRO: HTML5 API graphics

Plan❖Canvas❖SVG❖WebGL

Page 3: Academy PRO: HTML5 API graphics

Canvas

Page 4: Academy PRO: HTML5 API graphics

What is canva? z?

"Added in HTML5, the HTML <canvas> element can be used to draw graphics via scripting in JavaScript. For example, it can be used to draw graphs, make photo compositions, create animations, or even do real-time video processing or rendering.”

<canvas></canvas>

Page 5: Academy PRO: HTML5 API graphics

Coord system

Page 6: Academy PRO: HTML5 API graphics

Let’s look at example...

http://codepen.io/anon/pen/grWvmv

Page 7: Academy PRO: HTML5 API graphics

Canvas can be used inGamingAdvertisingData Representation.Education and Training.Art and Decoration..

Page 8: Academy PRO: HTML5 API graphics

Wikipedia гласит:

Page 9: Academy PRO: HTML5 API graphics

Why canvas?Unlike SVG is much more convenient to deal with a large number of elements;

It has hardware acceleration

You can manipulate each pixel

You can apply image processing filters

There are a lot of libraries

Page 10: Academy PRO: HTML5 API graphics

Why not?Loading the CPU and RAM;

Due to the limitations of the garbage collector, there is no way to clear the memory;

It is necessary to handle events with objects

Poor performance at high resolution

We have to draw in each element separately

Page 11: Academy PRO: HTML5 API graphics

Canvas contextvar canvas = document.getElementById("myCanvas");

var ctx = canvas.getContext("2d");

Page 12: Academy PRO: HTML5 API graphics

Default properties❖Size - 300x150

❖id

❖Checking for availability

Page 13: Academy PRO: HTML5 API graphics

Cute examplehttp://codepen.io/anon/pen/GZmozE

Page 15: Academy PRO: HTML5 API graphics

More Canvas exampleshttp://corehtml5canvas.com/code-live/

Page 17: Academy PRO: HTML5 API graphics

jCanvas example!http://codepen.io/SitePoint/pen/OMZNQx

Page 18: Academy PRO: HTML5 API graphics

Siteshttp://wineexplorer.brancottestate.com/attribute/varietal

https://www.google.com.ua/maps/

Page 19: Academy PRO: HTML5 API graphics

Lazy? Solution crazy!http://blog.mikeswanson.com/ai2canvas

Page 20: Academy PRO: HTML5 API graphics
Page 21: Academy PRO: HTML5 API graphics
Page 22: Academy PRO: HTML5 API graphics
Page 23: Academy PRO: HTML5 API graphics

SVG

Page 24: Academy PRO: HTML5 API graphics

SVG? (Schtirlitz V Gorode)

SVG is a language for describing two-dimensional graphics. As a standalone format or when mixed with other XML, it

uses the XML syntax [XML10]. When mixed with HTML5, it uses the HTML5 syntax [HTML]. …

SVG drawings can be interactive and dynamic. Animations can be defined and triggered either declaratively (i.e., by

embedding SVG animation elements in SVG content) or via scripting.

Page 25: Academy PRO: HTML5 API graphics

Coordinate system

Page 26: Academy PRO: HTML5 API graphics

Viewbox<svg width="800" height="600" viewbox="0 0 400 300">

<!-- SVG content drawn onto the SVG canvas -->

</svg>

Page 28: Academy PRO: HTML5 API graphics

SVGAny size - same quality

Search engine bots

Independence from JavaScript

Page 29: Academy PRO: HTML5 API graphics
Page 30: Academy PRO: HTML5 API graphics

SVG consBig file size.

Maps - small elements require reading whole doc.

Less details - more size

Page 31: Academy PRO: HTML5 API graphics

Let’s look

http://codepen.io/anon/pen/vGmdwj

Page 32: Academy PRO: HTML5 API graphics

Path<path d="M10 10 h 80 v 80 h -80 Z" fill="transparent" stroke="black"/>

The path will move to point (10,10) and then move horizontally 80 points to the right, then 80 points down, then 80 points to the left, and then back to the start.

https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths

Page 33: Academy PRO: HTML5 API graphics
Page 34: Academy PRO: HTML5 API graphics

SVG statisticshttp://w3techs.com/technologies/details/im-svg/all/all

Page 35: Academy PRO: HTML5 API graphics

SVG vs Icon Fonthttps://github.com/blog/2112-delivering-octicons-with-svg

Page 37: Academy PRO: HTML5 API graphics

D3// Update…var p = d3.select("body").selectAll("p") .data([4, 8, 15, 16, 23, 42]) .text(function(d) { return d; });

// Enter…p.enter().append("p") .text(function(d) { return d; });

// Exit…p.exit().remove();

Page 38: Academy PRO: HTML5 API graphics

Playhttp://sheav.se/en

Page 39: Academy PRO: HTML5 API graphics
Page 40: Academy PRO: HTML5 API graphics
Page 41: Academy PRO: HTML5 API graphics
Page 42: Academy PRO: HTML5 API graphics
Page 43: Academy PRO: HTML5 API graphics
Page 44: Academy PRO: HTML5 API graphics
Page 45: Academy PRO: HTML5 API graphics

Fight baby, fight!http://codepen.io/chris-creditdesign/details/BIADJ

Page 46: Academy PRO: HTML5 API graphics

So?

Page 47: Academy PRO: HTML5 API graphics

WebGL

Page 48: Academy PRO: HTML5 API graphics

What the heck is WebGL??WebGL allows web content to use the API, based on OpenGL ES 2.0, for rendering three-dimensional graphics without the use of plug-ins in the HTML canvas element in browsers that implement support for it. WebGL programs consist of control code written in JavaScript code and special effects (shader code) that runs on the GPU. WebGL elements can be mixed with other HTML elements, and assembled with other parts of a web page or a web page background.

Page 49: Academy PRO: HTML5 API graphics

WebGL is contextvar gl;try { gl = canvas.getContext("experimental-webgl");} catch(e) {}

Page 50: Academy PRO: HTML5 API graphics

UsesGLSL (OpenGL Shading Language)

Page 51: Academy PRO: HTML5 API graphics

WebGL prosShows a promise of 3D with dramatically lowered friction. No client or plugin install necessary.

Royalty-free open standard shepherded by the Kronos consortium -http://www.khronos.org/

Uses HTML5's canvas element and so is part of a larger change taking place on the web rather than an isolated technology.

Browser makers Mozilla, Opera, Apple & Google are all onboard.

Transparent development of spec. Majority of discussion takes place atwww.khronos.org/webgl/public-mailing-list/

Gives access to the power of a computer's graphics hardware from within scripts on web pages.

Page 52: Academy PRO: HTML5 API graphics

Web GL consNot yet available beyond betaversions of Firefox, Opera, Safari and Chrome browsers. Later this year

seems to be current best guess for spec hitting v1.0. Seems that the remaining issues are related to getting security right. Good news is that as soon as spec is signed of on then implementations should be ready to roll out.

Microsoft has not signed on. The way to get it to run in IE will likely by with the Chrome Frame plugin.

Depends on having OpenGL 2.0 drivers present which many computers will not have.http://code.google.com/p/anglepr... is an open source project by Google to address this issue.

Despite recent improvements Javascript may still feel too slow or just plain unfamiliar to people used to writing rich 3D applications.

Most developer libraries are still in an early stage. Partial list:http://www.khronos.org/webgl/wik...

Page 53: Academy PRO: HTML5 API graphics

Look insidehttp://codepen.io/kenjiSpecial/pen/vELOrM

Page 54: Academy PRO: HTML5 API graphics
Page 55: Academy PRO: HTML5 API graphics
Page 56: Academy PRO: HTML5 API graphics
Page 57: Academy PRO: HTML5 API graphics

DiffOne main difference is that a vertex shader can manipulate the attributes of vertices. which are the corner points of your polygons.

The fragment shader on the other hand takes care of how the pixels between the vertices look. They are interpolated between the defined vertices following specific rules.

Page 59: Academy PRO: HTML5 API graphics

To readhttps://hacks.mozilla.org/2013/04/the-concepts-of-webgl/

Page 60: Academy PRO: HTML5 API graphics

<script id="vshader" type="x-shader/x-vertex">

uniform mat4 u_modelViewProjMatrix; uniform mat4 u_normalMatrix; uniform vec3 lightDir;

attribute vec3 vNormal; attribute vec4 vTexCoord; attribute vec4 vPosition;

varying float v_Dot; varying vec2 v_texCoord;

void main() { gl_Position = u_modelViewProjMatrix * vPosition; v_texCoord = vTexCoord.st; vec4 transNormal = u_normalMatrix * vec4(vNormal, 1); v_Dot = max(dot(transNormal.xyz, lightDir), 0.0); }

</script>

Page 61: Academy PRO: HTML5 API graphics

<script id="fshader" type="x-shader/x-fragment">

precision mediump float;

uniform sampler2D sampler2d;

varying float v_Dot; varying vec2 v_texCoord;

void main() { vec2 texCoord = vec2(v_texCoord.s, 1.0 - v_texCoord.t); vec4 color = texture2D(sampler2d, texCoord); color += vec4(0.1, 0.1, 0.1, 1); gl_FragColor = vec4(color.xyz * v_Dot, color.a); }

Page 62: Academy PRO: HTML5 API graphics

WebGL libsglMatrix JavaScript Matrix and Vector library for High Performance WebGL apps

Sylvester An open source library for manipulating vectors and matrices. Not optimized for WebGL but extremely robust.

A little bit more https://www.khronos.org/webgl/wiki/User_Contributions#Frameworks

Page 63: Academy PRO: HTML5 API graphics

Complexityhttps://github.com/mdn/webgl-examples/tree/gh-pages/tutorial/sample6

https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial/Using_textures_in_WebGL

Page 64: Academy PRO: HTML5 API graphics

WebGL exampleshttps://jayweeks.com/medusae/

http://osr.org/oms/

http://madebyevan.com/webgl-water/

http://labs.gooengine.com/pearl-boy/

Page 65: Academy PRO: HTML5 API graphics