SlideShare a Scribd company logo
1 of 49
SVG, Canvas e
Animations in
Angular
Leonardo Buscemi
Agenda
Premessa
SVG CANVAS
Librerie terze parti
Confronti
Angular compila il nostro codice
Tutto quello che scriviamo nella nostra applicazione
Angular (componenti, logica e stili) viene ‘compilato’
in codice Javascript.
Elementi SVG
<rect x="120" y="0" width="100" height="100" rx="0" ry="0" />
<circle cx="50" cy="50" r="50"/>
<polygon points="20,0 80,0 100,50 0,50" />
<path d="M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z"/>
SVG Elements
Simple (static) Component with SVG
import { Component } from @angular/core';
@Component({
selector: 'my-component',
template: `
<svg viewBox="0 0 250 250">
<g>
<polygon class="st0" fill="#DD0031" points="125,153 … 153.4" />
<polygon class="st0" fill="#DD0031" points="108,135 … 125,94.5" />
</g>
<g class="st1">
<polygon class="st0" fill="#C3002F" points="125,153 … 3.4" />
<polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" />
</g>
</svg> `,
})
export class AppComponent {}
Angular needs to know is SVG
<svg:rect x="0" y="0" width="100" height="100"/>
“An SVG snippet template needs an svg: prefix on its root element to disambiguate the SVG
element from an HTML component.”
Fonte
Fonte
Simple (static) Component with SVG
import { Component } from @angular/core';
@Component({
selector: 'my-component',
template: `
<svg viewBox="0 0 250 250">
<g>
<polygon class="st0" fill="#DD0031" points="125,153 … 153.4" />
<polygon class="st0" fill="#DD0031" points="108,135 … 125,94.5" />
</g>
<g class="st1">
<polygon class="st0" fill="#C3002F" points="125,153 … 3.4" />
<polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" />
</g>
</svg> `,
})
export class AppComponent {}
Simple (static) Component with SVG
import { Component } from @angular/core';
@Component({
selector: 'my-component',
template: `
<svg viewBox="0 0 250 250">
<svg:g>
<svg:polygon class="st0" fill="#DD0031" points="125,153 … 153.4" />
<svg:polygon class="st0" fill="#DD0031" points="108,135 … 125,94.5" />
</svg:g>
<svg:g class="st1">
<svg:polygon class="st0" fill="#C3002F" points="125,153 … 3.4" />
<svg:polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" />
</svg:g>
</svg> `,
})
export class AppComponent {}
Esempio live
https://stackblitz.com/edit/angular-svg-static
Component with SVG
import { Component } from @angular/core';
@Component({
selector: 'my-component',
template: `
<svg viewBox="0 0 250 250">
<svg:g>
<svg:polygon class="st0" fill="#DD0031" points="125,153 … 153.4" />
<svg:polygon class="st0" fill="#DD0031" points="108,135 … 125,94.5" />
</g>
<svg:g class="st1">
<svg:polygon class="st0" fill="#C3002F" points="125,153 … 3.4" />
<svg:polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" />
</g>
</svg> `,
})
export class AppComponent {}
Component with SVG
import { Component } from @angular/core';
@Component({
selector: 'my-component',
template: `
<svg viewBox="0 0 250 250">
<svg:g>
<svg:polygon class="st0" fill="#DD0031" points="125,153 … 153.4" />
<svg:polygon class="st0" fill="#DD0031" [points]="someMagic()" />
</g>
<svg:g class="st1">
<svg:polygon class="st0" fill="#C3002F" points="125,153 … 3.4" />
<svg:polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" />
</g>
</svg> `,
})
export class AppComponent {}
Component with SVG
import { Component } from @angular/core';
@Component({
selector: 'my-component',
template: `
<svg viewBox="0 0 250 250">
<svg:g>
<svg:polygon class="st0" fill="#DD0031" points="125,153 … 153.4" />
<svg:polygon class="st0" fill="#DD0031" [points]="someMagic()" />
</g>
<svg:g class="st1">
<svg:polygon class="st0" fill="#C3002F" points="125,153 … 3.4" />
<svg:polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" />
</g>
</svg> `,
})
export class AppComponent {}
HTML attribute vs. DOM property
❖ Alcuni attributi HTML hanno un mapping 1: 1 alle proprietà
➢ Ad esempio id
❖ Alcuni attributi HTML non hanno proprietà corrispondenti
➢ Ad esempio colspan e tutti gli attributi svg
❖ Alcune proprietà DOM non hanno attributi corrispondenti
➢ Ad esempio textContent
❖ Molti attributi HTML sembrano mappare alle proprietà ... ma non nel modo in cui si potrebbe pensare!
Gli attributi inizializzano le proprietà del DOM, queste possono cambiare; i valori degli attributi no.
Fonte
Un mondo senza attributi
In Angular l'unico scopo degli attributi è quello di inizializzare lo stato degli elementi e direttive, una volta
compilati gli attributi spariscono!
Fonte
Fonte
È necessario utilizzare il binding dell'attributo quando non esiste alcuna proprietà dell'elemento da associare.
Component with SVG
import { Component } from @angular/core';
@Component({
selector: 'my-component',
template: `
<svg viewBox="0 0 250 250">
<svg:g>
<svg:polygon class="st0" fill="#DD0031" points="125,153 … 153.4" />
<svg:polygon class="st0" fill="#DD0031" [attr.points]="someMagic()" />
</g>
<svg:g class="st1">
<svg:polygon class="st0" fill="#C3002F" points="125,153 … 3.4" />
<svg:polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" />
</g>
</svg> `,
})
export class AppComponent {}
Esempio live
https://stackblitz.com/edit/angular-svg-attribute
Suddivisione in componenti
import { Component } from '@angular/core';
@Component({
selector: 'my-part-left',
template: `
<svg:g >
<svg:polygon fill="#DD0031"
points="125,153.4 … 125,94.5" />
</svg:g>`
})
export class PartLeftComponent {
}
import { Component } from '@angular/core';
@Component({
selector: 'my-part-right',
template: `
<svg:g >
<svg:polygon fill="#C3002F"
points="125,153.4 … 125,94.5" />
</svg:g>`
})
export class PartRightComponent {
}
Suddivisione in componenti
<svg viewBox="0 0 250 250">
<my-part-left></my-part-left>
<my-part-right></my-part-right>
</svg>
Non viene renderizzato!
Selettore per attributo
import { Component } from '@angular/core';
@Component({
selector: '[my-part-left]',
template: `
<svg:g >
<svg:polygon fill="#DD0031"
points="125,153.4 ... 125,94.5" />
</svg:g>`
})
export class PartLeftComponent {
}
import { Component } from '@angular/core';
@Component({
selector: '[my-part-right]',
template: `
<svg:g >
<svg:polygon fill="#C3002F"
points="125,153.4 125,94.5" />
</svg:g>`
})
export class PartRightComponent {
}
Suddivisione in componenti OK
<svg viewBox="0 0 250 250">
<svg:g my-part-left />
<svg:g my-part-right />
</svg>
Esempio live
https://stackblitz.com/edit/angular-svg-components
Animazioni su SVG
● SMIL
○ Deprecato
● CSS
● Angular Animation
● Javascript
○ Green Sock
Esempio live
Animazione di SVG in CSS
https://stackblitz.com/edit/angular-svg-components
Angular Animation
npm i @angular/animations
app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, BrowserAnimationsModule, FormsModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Esempio live
Animazione di SVG con
Angular Animation
https://stackblitz.com/edit/angular-svg-anim-ng
Animazione in Javascript
Vogliamo animare anche la proprietà points del nostro polygon
@ViewChild e
template reference
https://stackblitz.com/edit/viewchild-template-ref
Greensock
npm i gsap @types/greensock
import { Component, ViewChild, ElementRef } from '@angular/core';
import 'gsap';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
})
export class AppComponent {
@ViewChild('left') left: ElementRef;
@ViewChild('right') right: ElementRef;
ngOnInit() {
TweenMax.to(this.left.nativeElement, 2, {
attr: {
points: "125,153.4 … 125,195"
},
repeat: -1, yoyo: true, ease: Cubic.easeInOut
});
TweenMax.to(this.right.nativeElement, 2, {
attr: {
points: "125,153.4 … 125,153.4"
},
repeat: -1, yoyo: true, ease: Cubic.easeInOut
});
}
}
Esempio live
Animazione di SVG con GreenSock
https://stackblitz.com/edit/angular-svg-static-grensock
Canvas
SVG usa il modello “retained”
Fonte
È un modello dichiarativo. L’applicazione costruisce la scena utilizzando elementi
grafici (linee, forme) della libreria grafica che si occupa di creare il modello della scena
e inviare i comandi di disegno.
Canvas usa il modello ‘immediate’
Fonte
È un modello procedurale. L’applicazione invia direttamente i comandi di disegno e
deve tenere traccia del modello della scena
“Simple” Canvas component
import { Component, ViewChild, ElementRef } from '@angular/core';
@Component({
selector: 'my-app',
template: '<canvas #myCanvas height="200" width="200" ></canvas>'
})
export class AppComponent {
@ViewChild("myCanvas") canvasRef: ElementRef;
private ctx: CanvasRenderingContext2D;
ngOnInit() {
this.ctx = this.canvasRef.nativeElement.getContext('2d');
this.drawBorder();
this.drawPoint();
}
drawPoint() {
this.ctx.beginPath();
…
this.ctx.fill();
}
drawBorder() {
this.ctx.beginPath();
…
this.ctx.stroke();
}
}
Esempio live
Componente con canvas
https://stackblitz.com/edit/angular-canvas-simple
Esempio live
Animazione con canvas
https://stackblitz.com/edit/angular-canvas-animation
Librerie terze parti
SVG
● Raphael
● SnapSVG
● D3
CANVAS
● EaselJS
● ChartJS
● ThreeJS
Raphael
SnapSVG
D3.js
EaselJS
ChartJS
ThreeJS
Confronto SVG ⇔ Canvas
SVG CANVAS
Grafica Vettoriale Raster
Rendering Retained Immediate*
Utilizzo Markup XML Low level API
Quale scegliere ?
https://technet.microsoft.com/it-it/gg193983(v=vs.71)
Leonardo Buscemi
Software Developer presso
https://www.linkedin.com/in/leonardobuscemi/
https://www.facebook.com/leonardo.buscemi
https://twitter.com/leonardobuscemi
GRAZIE

More Related Content

What's hot

AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - ServicesNir Kaufman
 
Angular Mini-Challenges
Angular Mini-ChallengesAngular Mini-Challenges
Angular Mini-ChallengesJose Mendez
 
The battle of Protractor and Cypress - RunIT Conference 2019
The battle of Protractor and Cypress - RunIT Conference 2019The battle of Protractor and Cypress - RunIT Conference 2019
The battle of Protractor and Cypress - RunIT Conference 2019Ludmila Nesvitiy
 
How to build an AngularJS backend-ready app WITHOUT BACKEND
How to build an AngularJS backend-ready app WITHOUT BACKEND How to build an AngularJS backend-ready app WITHOUT BACKEND
How to build an AngularJS backend-ready app WITHOUT BACKEND Enrique Oriol Bermúdez
 
AngularJS best-practices
AngularJS best-practicesAngularJS best-practices
AngularJS best-practicesHenry Tao
 
AngularJS for designers and developers
AngularJS for designers and developersAngularJS for designers and developers
AngularJS for designers and developersKai Koenig
 
Styling recipes for Angular components
Styling recipes for Angular componentsStyling recipes for Angular components
Styling recipes for Angular componentsNir Kaufman
 
AngularJS Basics
AngularJS BasicsAngularJS Basics
AngularJS BasicsRavi Mone
 
AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.Yan Yankowski
 
ChtiJUG - Introduction à Angular2
ChtiJUG - Introduction à Angular2ChtiJUG - Introduction à Angular2
ChtiJUG - Introduction à Angular2Demey Emmanuel
 
Technozaure - Angular2
Technozaure - Angular2Technozaure - Angular2
Technozaure - Angular2Demey Emmanuel
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injectionEyal Vardi
 
Building Reusable Custom Elements With Angular
Building Reusable Custom Elements With AngularBuilding Reusable Custom Elements With Angular
Building Reusable Custom Elements With AngularIlia Idakiev
 
Beyond AngularJS: Best practices and more
Beyond AngularJS: Best practices and moreBeyond AngularJS: Best practices and more
Beyond AngularJS: Best practices and moreAri Lerner
 
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDBDynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDBApaichon Punopas
 
Top 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppTop 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppKaty Slemon
 

What's hot (20)

AngularJS - Services
AngularJS - ServicesAngularJS - Services
AngularJS - Services
 
Angular Mini-Challenges
Angular Mini-ChallengesAngular Mini-Challenges
Angular Mini-Challenges
 
The battle of Protractor and Cypress - RunIT Conference 2019
The battle of Protractor and Cypress - RunIT Conference 2019The battle of Protractor and Cypress - RunIT Conference 2019
The battle of Protractor and Cypress - RunIT Conference 2019
 
How to build an AngularJS backend-ready app WITHOUT BACKEND
How to build an AngularJS backend-ready app WITHOUT BACKEND How to build an AngularJS backend-ready app WITHOUT BACKEND
How to build an AngularJS backend-ready app WITHOUT BACKEND
 
AngularJS best-practices
AngularJS best-practicesAngularJS best-practices
AngularJS best-practices
 
Solid angular
Solid angularSolid angular
Solid angular
 
AngularJS for designers and developers
AngularJS for designers and developersAngularJS for designers and developers
AngularJS for designers and developers
 
Angularjs
AngularjsAngularjs
Angularjs
 
Styling recipes for Angular components
Styling recipes for Angular componentsStyling recipes for Angular components
Styling recipes for Angular components
 
AngularJS Basics
AngularJS BasicsAngularJS Basics
AngularJS Basics
 
AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.
 
ChtiJUG - Introduction à Angular2
ChtiJUG - Introduction à Angular2ChtiJUG - Introduction à Angular2
ChtiJUG - Introduction à Angular2
 
Technozaure - Angular2
Technozaure - Angular2Technozaure - Angular2
Technozaure - Angular2
 
Angular 2.0 Dependency injection
Angular 2.0 Dependency injectionAngular 2.0 Dependency injection
Angular 2.0 Dependency injection
 
Building Reusable Custom Elements With Angular
Building Reusable Custom Elements With AngularBuilding Reusable Custom Elements With Angular
Building Reusable Custom Elements With Angular
 
Beyond AngularJS: Best practices and more
Beyond AngularJS: Best practices and moreBeyond AngularJS: Best practices and more
Beyond AngularJS: Best practices and more
 
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDBDynamic Application Development by NodeJS ,AngularJS with OrientDB
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
 
Top 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular AppTop 7 Angular Best Practices to Organize Your Angular App
Top 7 Angular Best Practices to Organize Your Angular App
 
Angular js-crash-course
Angular js-crash-courseAngular js-crash-course
Angular js-crash-course
 
AngularJS Workshop
AngularJS WorkshopAngularJS Workshop
AngularJS Workshop
 

Similar to Svg, canvas e animations in angular (3 maggio 2019)

angular fundamentals.pdf angular fundamentals.pdf
angular fundamentals.pdf angular fundamentals.pdfangular fundamentals.pdf angular fundamentals.pdf
angular fundamentals.pdf angular fundamentals.pdfNuttavutThongjor1
 
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...Guillaume Kossi
 
A Gentle Introduction to Angular Schematics - Angular SF 2019
A Gentle Introduction to Angular Schematics - Angular SF 2019A Gentle Introduction to Angular Schematics - Angular SF 2019
A Gentle Introduction to Angular Schematics - Angular SF 2019Matt Raible
 
Animating an svg icon
Animating an svg iconAnimating an svg icon
Animating an svg icondeepbidis
 
Use Angular Schematics to Simplify Your Life - Develop Denver 2019
Use Angular Schematics to Simplify Your Life - Develop Denver 2019Use Angular Schematics to Simplify Your Life - Develop Denver 2019
Use Angular Schematics to Simplify Your Life - Develop Denver 2019Matt Raible
 
Angular js 2.0, ng poznań 20.11
Angular js 2.0, ng poznań 20.11Angular js 2.0, ng poznań 20.11
Angular js 2.0, ng poznań 20.11Kamil Augustynowicz
 
Animating angular applications
Animating angular applicationsAnimating angular applications
Animating angular applicationsTomek Sułkowski
 
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019Matt Raible
 
はじめてのAngular2
はじめてのAngular2はじめてのAngular2
はじめてのAngular2Kenichi Kanai
 
SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)Oswald Campesato
 
AngularJS Animations
AngularJS AnimationsAngularJS Animations
AngularJS AnimationsEyal Vardi
 
Angular 4 for Java Developers
Angular 4 for Java DevelopersAngular 4 for Java Developers
Angular 4 for Java DevelopersYakov Fain
 
Building a TV show with Angular, Bootstrap, and Web Services
Building a TV show with Angular, Bootstrap, and Web ServicesBuilding a TV show with Angular, Bootstrap, and Web Services
Building a TV show with Angular, Bootstrap, and Web ServicesDavid Giard
 
PWA night vol.11 20191218
PWA night vol.11 20191218PWA night vol.11 20191218
PWA night vol.11 20191218bitpart
 

Similar to Svg, canvas e animations in angular (3 maggio 2019) (20)

angular fundamentals.pdf angular fundamentals.pdf
angular fundamentals.pdf angular fundamentals.pdfangular fundamentals.pdf angular fundamentals.pdf
angular fundamentals.pdf angular fundamentals.pdf
 
SVG introduction
SVG   introductionSVG   introduction
SVG introduction
 
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...
Sara Soueidan: Styling and Animating Scalable Vector Graphics with CSS [CSSCo...
 
A Gentle Introduction to Angular Schematics - Angular SF 2019
A Gentle Introduction to Angular Schematics - Angular SF 2019A Gentle Introduction to Angular Schematics - Angular SF 2019
A Gentle Introduction to Angular Schematics - Angular SF 2019
 
Animating an svg icon
Animating an svg iconAnimating an svg icon
Animating an svg icon
 
Web Vector Graphics
Web Vector GraphicsWeb Vector Graphics
Web Vector Graphics
 
Use Angular Schematics to Simplify Your Life - Develop Denver 2019
Use Angular Schematics to Simplify Your Life - Develop Denver 2019Use Angular Schematics to Simplify Your Life - Develop Denver 2019
Use Angular Schematics to Simplify Your Life - Develop Denver 2019
 
Angular js 2.0, ng poznań 20.11
Angular js 2.0, ng poznań 20.11Angular js 2.0, ng poznań 20.11
Angular js 2.0, ng poznań 20.11
 
angular fundamentals.pdf
angular fundamentals.pdfangular fundamentals.pdf
angular fundamentals.pdf
 
Animating angular applications
Animating angular applicationsAnimating angular applications
Animating angular applications
 
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019
A Gentle Introduction to Angular Schematics - Devoxx Belgium 2019
 
はじめてのAngular2
はじめてのAngular2はじめてのAngular2
はじめてのAngular2
 
Vue js and Dyploma
Vue js and DyplomaVue js and Dyploma
Vue js and Dyploma
 
Svcc 2013-d3
Svcc 2013-d3Svcc 2013-d3
Svcc 2013-d3
 
SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)SVCC 2013 D3.js Presentation (10/05/2013)
SVCC 2013 D3.js Presentation (10/05/2013)
 
AngularJS Animations
AngularJS AnimationsAngularJS Animations
AngularJS Animations
 
SVGD3Angular2React
SVGD3Angular2ReactSVGD3Angular2React
SVGD3Angular2React
 
Angular 4 for Java Developers
Angular 4 for Java DevelopersAngular 4 for Java Developers
Angular 4 for Java Developers
 
Building a TV show with Angular, Bootstrap, and Web Services
Building a TV show with Angular, Bootstrap, and Web ServicesBuilding a TV show with Angular, Bootstrap, and Web Services
Building a TV show with Angular, Bootstrap, and Web Services
 
PWA night vol.11 20191218
PWA night vol.11 20191218PWA night vol.11 20191218
PWA night vol.11 20191218
 

Recently uploaded

Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 

Recently uploaded (20)

Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 

Svg, canvas e animations in angular (3 maggio 2019)

  • 1. SVG, Canvas e Animations in Angular Leonardo Buscemi
  • 3. Angular compila il nostro codice Tutto quello che scriviamo nella nostra applicazione Angular (componenti, logica e stili) viene ‘compilato’ in codice Javascript.
  • 4.
  • 5. Elementi SVG <rect x="120" y="0" width="100" height="100" rx="0" ry="0" /> <circle cx="50" cy="50" r="50"/> <polygon points="20,0 80,0 100,50 0,50" /> <path d="M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z"/> SVG Elements
  • 6. Simple (static) Component with SVG import { Component } from @angular/core'; @Component({ selector: 'my-component', template: ` <svg viewBox="0 0 250 250"> <g> <polygon class="st0" fill="#DD0031" points="125,153 … 153.4" /> <polygon class="st0" fill="#DD0031" points="108,135 … 125,94.5" /> </g> <g class="st1"> <polygon class="st0" fill="#C3002F" points="125,153 … 3.4" /> <polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" /> </g> </svg> `, }) export class AppComponent {}
  • 7. Angular needs to know is SVG <svg:rect x="0" y="0" width="100" height="100"/> “An SVG snippet template needs an svg: prefix on its root element to disambiguate the SVG element from an HTML component.” Fonte
  • 9. Simple (static) Component with SVG import { Component } from @angular/core'; @Component({ selector: 'my-component', template: ` <svg viewBox="0 0 250 250"> <g> <polygon class="st0" fill="#DD0031" points="125,153 … 153.4" /> <polygon class="st0" fill="#DD0031" points="108,135 … 125,94.5" /> </g> <g class="st1"> <polygon class="st0" fill="#C3002F" points="125,153 … 3.4" /> <polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" /> </g> </svg> `, }) export class AppComponent {}
  • 10. Simple (static) Component with SVG import { Component } from @angular/core'; @Component({ selector: 'my-component', template: ` <svg viewBox="0 0 250 250"> <svg:g> <svg:polygon class="st0" fill="#DD0031" points="125,153 … 153.4" /> <svg:polygon class="st0" fill="#DD0031" points="108,135 … 125,94.5" /> </svg:g> <svg:g class="st1"> <svg:polygon class="st0" fill="#C3002F" points="125,153 … 3.4" /> <svg:polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" /> </svg:g> </svg> `, }) export class AppComponent {}
  • 12.
  • 13. Component with SVG import { Component } from @angular/core'; @Component({ selector: 'my-component', template: ` <svg viewBox="0 0 250 250"> <svg:g> <svg:polygon class="st0" fill="#DD0031" points="125,153 … 153.4" /> <svg:polygon class="st0" fill="#DD0031" points="108,135 … 125,94.5" /> </g> <svg:g class="st1"> <svg:polygon class="st0" fill="#C3002F" points="125,153 … 3.4" /> <svg:polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" /> </g> </svg> `, }) export class AppComponent {}
  • 14. Component with SVG import { Component } from @angular/core'; @Component({ selector: 'my-component', template: ` <svg viewBox="0 0 250 250"> <svg:g> <svg:polygon class="st0" fill="#DD0031" points="125,153 … 153.4" /> <svg:polygon class="st0" fill="#DD0031" [points]="someMagic()" /> </g> <svg:g class="st1"> <svg:polygon class="st0" fill="#C3002F" points="125,153 … 3.4" /> <svg:polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" /> </g> </svg> `, }) export class AppComponent {}
  • 15. Component with SVG import { Component } from @angular/core'; @Component({ selector: 'my-component', template: ` <svg viewBox="0 0 250 250"> <svg:g> <svg:polygon class="st0" fill="#DD0031" points="125,153 … 153.4" /> <svg:polygon class="st0" fill="#DD0031" [points]="someMagic()" /> </g> <svg:g class="st1"> <svg:polygon class="st0" fill="#C3002F" points="125,153 … 3.4" /> <svg:polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" /> </g> </svg> `, }) export class AppComponent {}
  • 16. HTML attribute vs. DOM property ❖ Alcuni attributi HTML hanno un mapping 1: 1 alle proprietà ➢ Ad esempio id ❖ Alcuni attributi HTML non hanno proprietà corrispondenti ➢ Ad esempio colspan e tutti gli attributi svg ❖ Alcune proprietà DOM non hanno attributi corrispondenti ➢ Ad esempio textContent ❖ Molti attributi HTML sembrano mappare alle proprietà ... ma non nel modo in cui si potrebbe pensare! Gli attributi inizializzano le proprietà del DOM, queste possono cambiare; i valori degli attributi no. Fonte
  • 17. Un mondo senza attributi In Angular l'unico scopo degli attributi è quello di inizializzare lo stato degli elementi e direttive, una volta compilati gli attributi spariscono! Fonte Fonte È necessario utilizzare il binding dell'attributo quando non esiste alcuna proprietà dell'elemento da associare.
  • 18. Component with SVG import { Component } from @angular/core'; @Component({ selector: 'my-component', template: ` <svg viewBox="0 0 250 250"> <svg:g> <svg:polygon class="st0" fill="#DD0031" points="125,153 … 153.4" /> <svg:polygon class="st0" fill="#DD0031" [attr.points]="someMagic()" /> </g> <svg:g class="st1"> <svg:polygon class="st0" fill="#C3002F" points="125,153 … 3.4" /> <svg:polygon class="st0" fill="#C3002F" points="142,135.4 … 94.5" /> </g> </svg> `, }) export class AppComponent {}
  • 20. Suddivisione in componenti import { Component } from '@angular/core'; @Component({ selector: 'my-part-left', template: ` <svg:g > <svg:polygon fill="#DD0031" points="125,153.4 … 125,94.5" /> </svg:g>` }) export class PartLeftComponent { } import { Component } from '@angular/core'; @Component({ selector: 'my-part-right', template: ` <svg:g > <svg:polygon fill="#C3002F" points="125,153.4 … 125,94.5" /> </svg:g>` }) export class PartRightComponent { }
  • 21. Suddivisione in componenti <svg viewBox="0 0 250 250"> <my-part-left></my-part-left> <my-part-right></my-part-right> </svg> Non viene renderizzato!
  • 22. Selettore per attributo import { Component } from '@angular/core'; @Component({ selector: '[my-part-left]', template: ` <svg:g > <svg:polygon fill="#DD0031" points="125,153.4 ... 125,94.5" /> </svg:g>` }) export class PartLeftComponent { } import { Component } from '@angular/core'; @Component({ selector: '[my-part-right]', template: ` <svg:g > <svg:polygon fill="#C3002F" points="125,153.4 125,94.5" /> </svg:g>` }) export class PartRightComponent { }
  • 23. Suddivisione in componenti OK <svg viewBox="0 0 250 250"> <svg:g my-part-left /> <svg:g my-part-right /> </svg>
  • 25. Animazioni su SVG ● SMIL ○ Deprecato ● CSS ● Angular Animation ● Javascript ○ Green Sock
  • 26. Esempio live Animazione di SVG in CSS https://stackblitz.com/edit/angular-svg-components
  • 27.
  • 28. Angular Animation npm i @angular/animations app.module.ts import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule, BrowserAnimationsModule, FormsModule ], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export class AppModule { }
  • 29. Esempio live Animazione di SVG con Angular Animation https://stackblitz.com/edit/angular-svg-anim-ng
  • 30. Animazione in Javascript Vogliamo animare anche la proprietà points del nostro polygon
  • 32. Greensock npm i gsap @types/greensock import { Component, ViewChild, ElementRef } from '@angular/core'; import 'gsap'; @Component({ selector: 'my-app', templateUrl: './app.component.html', }) export class AppComponent { @ViewChild('left') left: ElementRef; @ViewChild('right') right: ElementRef; ngOnInit() { TweenMax.to(this.left.nativeElement, 2, { attr: { points: "125,153.4 … 125,195" }, repeat: -1, yoyo: true, ease: Cubic.easeInOut }); TweenMax.to(this.right.nativeElement, 2, { attr: { points: "125,153.4 … 125,153.4" }, repeat: -1, yoyo: true, ease: Cubic.easeInOut }); } }
  • 33. Esempio live Animazione di SVG con GreenSock https://stackblitz.com/edit/angular-svg-static-grensock
  • 35. SVG usa il modello “retained” Fonte È un modello dichiarativo. L’applicazione costruisce la scena utilizzando elementi grafici (linee, forme) della libreria grafica che si occupa di creare il modello della scena e inviare i comandi di disegno.
  • 36. Canvas usa il modello ‘immediate’ Fonte È un modello procedurale. L’applicazione invia direttamente i comandi di disegno e deve tenere traccia del modello della scena
  • 37. “Simple” Canvas component import { Component, ViewChild, ElementRef } from '@angular/core'; @Component({ selector: 'my-app', template: '<canvas #myCanvas height="200" width="200" ></canvas>' }) export class AppComponent { @ViewChild("myCanvas") canvasRef: ElementRef; private ctx: CanvasRenderingContext2D; ngOnInit() { this.ctx = this.canvasRef.nativeElement.getContext('2d'); this.drawBorder(); this.drawPoint(); } drawPoint() { this.ctx.beginPath(); … this.ctx.fill(); } drawBorder() { this.ctx.beginPath(); … this.ctx.stroke(); } }
  • 38. Esempio live Componente con canvas https://stackblitz.com/edit/angular-canvas-simple
  • 39. Esempio live Animazione con canvas https://stackblitz.com/edit/angular-canvas-animation
  • 40. Librerie terze parti SVG ● Raphael ● SnapSVG ● D3 CANVAS ● EaselJS ● ChartJS ● ThreeJS
  • 43. D3.js
  • 47. Confronto SVG ⇔ Canvas SVG CANVAS Grafica Vettoriale Raster Rendering Retained Immediate* Utilizzo Markup XML Low level API
  • 49. Leonardo Buscemi Software Developer presso https://www.linkedin.com/in/leonardobuscemi/ https://www.facebook.com/leonardo.buscemi https://twitter.com/leonardobuscemi GRAZIE

Editor's Notes

  1. Spiegare icona Angular in alto
  2. Il link punta alla pagina di Mozilla
  3. Spiegare subito perchè
  4. Adesso vogliamo rendere dinamica una parte del nostro componente
  5. $0.getAttribute("value") $0.value