Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
1K views
8 pages
Alternative Analog SVG Clock
Uploaded by
Leonardo Forero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save Alternative Analog SVG Clock For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
1K views
8 pages
Alternative Analog SVG Clock
Uploaded by
Leonardo Forero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save Alternative Analog SVG Clock For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save Alternative Analog SVG Clock For Later
You are on page 1
/ 8
Search
Fullscreen
ee)b) = PROJECT Alternative Analog SVG Clock Sergey Alexandrovich Kryukov 14 Mar 2023 CPOL A pure Web browser application, an alternative to the article “An SVG Analog Clock" The application has no server side and is based on HTML + SVG + CSS + JavaScript only Download source code — 3.8 KB se Analog SVG x CG @ File| C/sa/papers/My/Code.. 1 oe OF & Alternative Analog SVG Clock __ Copyright © Sergey A Kryukov, 2023Contents Introduction What's so Interesting? Implementation svG Clock Comparison of Object Types Arrow Rotation Main Difference Between Analog and Digital Clocks How to Add Decorations? CSS: Docking Layout CSS: Fitting SVG What About Server Part? Introduction What's so Interesting? What's so Interesting? Virtually nothing! This is the application alternative to the one shown in the atticle An SVG Analog Clock. | was surprised by the fact of how a clock could be over-engineered and decided to provide a lean and near-optimal solution which | think is way more adequate ‘An on-screen clock has near-zero practical value, maybe even negative. This is just a traditional programming exercise, on par with editors, Tetris implementations, fractal image rendering, and the like. However, this article explains several subtle points that could be useful for beginners. Implementation SVG First, let's see how SVG can be built dynamically. Everything is implemented as the class SVG: JavaScript class SVG ( implementation = (};constructor (viewBox, existingSvg) { const ns = “http://www.w3.org/2000/svg" ; this.#implementation.createNS = (name) =: document .createElementNS(ns, name) ; this.#implementation.attribute = (element, values) => { for (let attribute in values) element. setattribute(attribute, values[attribute]); return element; }5 //this. #impLenentation. attribute // defaults... this.#implementation.svg = existingSvg == null ? this.i#implenentation.createns("svg") : existingSvg; if (viewBox I= null) // it also covers undefined this.d#implenentation. svg. setattribute("viewBox", $(view8ox.x. from} ${viewBox.y. from} ${viewBox.x.to - viewBox.x. from} ${viewBox.y.to - viewBox.y. from)” )5 } //constructor get element() { return this.#implementation.svg; } // ... get/set attributes, primitives: Line, circle, ets. appendElement(elenent, group) { if (group) group. appendchild(element) ; else ‘this. #implementation. svg. appendchild(elenent) ; return elements } //appendéLement group() { return this.appendElement(this.#implementation.createNs("g")); } } //class SvG First, note that all the private code is hidden in the constructor and the private member. ‘this. #implementation ‘An SVG element can be created using the document object in a bit different way than HTML elements because SVG uses a different namespace. This is implemented in a local method this. itimplementation.createns. The class SVG can be used in two different ways. If existingSvg is specified, the class can be used to populate an existing
element, and new content can be added to existing content. It can be used to add content to an
element embedded in HTML. Clock The function createClock creates an instance of the clock. It can also be created in two different ways. If the parameter parent is of the class SVG (described above), the clock can be rendered on top of some existing
element. Otherwise, it is created from scratch. As the instance of SVG.element isappended to some existing HTML element, the entire clock created from scratch will be appended to it. The function returns the function set used to set and render the current clock time. To choose the way to use, the code needs to compare objects by type. Let's look at this Comparison of Object Types In the function createClock, we have: JavaScript if (parent.constructor I= SVG) parent .appendChild(svg.element) ; Naturally, at this point, we assume that the object parent is not undefined and not null. When needed, it can be checked up by if (parent != null), as this comparison also covers the case of undefined. This is the way to compare types directly, not using magic strings. We can find many examples of JavaScript code where the types are compared using type names. This is dirty, less maintainable, and should be avoided. Arrow Rotation The central part of the clock is the method set. It rotates the arrows: JavaScript createClock parent => { const set = time => { if (currentTime currentTime = time; const date = new Date(time); let hour = date.getHours() % 12; let minute = date.getMinutes(); let second = date.getSeconds(); hour = (hour + minute / 69 + second / 3600) / 12; minute = (minute + second / 60) / 60; second = second / 68; arrowHour.style. transform = “rotate(${hour}turn)”; arrowMinute.style.transform = “rotate(${minute}turn)” 5 arrowSecond.style.transform = “rotate(${second}turn) 5 ys //set time) return; return set }3 //createClockHere, arrowHour, arrowMinute, arrowSecond are SVG groups. Why? It is done to generalize the code and make it maintainable in case one needs to render some complicated arrow shapes composed of several SVG elements. The other role of style. transform is to avoid trigonometric calculations and make the code more maintainable. Note that the transform assumes that the coordinate point (0, 0) is the center of rotation, Main Difference Between Analog and Digital Clocks The first idea of the calculation of the arrow angles would be using just hours, minutes, and seconds. This is what a digital clock does. These values are integers. And yes, it would be suitable for a digital clock, An analog clock is different. It would be incorrect to say that an hour arrow shows just hours. It should show Thi JavaScript createClock = parent => ( const set = time => { if (currentTime currentTime = time; const date = new Date(time); et hour = date.getHours() % 12; let minute = date.getminutes(); let second = date.getSeconds(); hour = hour / 125 minute = minute / 69; second = second / 69; arrowHour.style.transform = “rotate(${hour}turn)” 5 arrowtinute. style.transform = “rotate(${minute}turn)” 5 arrowSecond.style.transform = ~rotate(${second}turn)” 5 hs //set time) return; return sets }5 //createclock If this calculation was the same as for a digital clock, the minute arrow would stay at some minute label and jump to a new position every minute. And the hour arrow would jump by an hour every hour. Instead, all the arrows should move by seconds. This is done by the correct calculation shown above. How to Add Decorations?The demo application is very simple, How to add all those Arabic or Roman numeric labels, fancy arrows, or fancy backgrounds? The way to go would be using an existing
element embedded in HTML. The SVG elements to be controlled by the script could be added on top of it. When | need to do such a thing, | draw appropriate vector graphics with some vector graphic editor and save it as an SVG file, | would recommend Inkscape. The file can be embedded into HTML. Usually, the file should be cleaned of comments, redundant metadata, and unused id attributes. Numeric colors should better be replaced with CSS color names Typically, adding some vector graphics to existing SVG requires some group. The reference to it can be obtained via its id attribute or some other way using Document .querySelector(). Here, most important thing is to match the coordinate system and a viewport. CSS: Docking Layout Note that the application behaves pretty much like a “desktop application”: when a browser window is resized, the
and
You might also like
IOS Drawing Practical UIKit Solutions Mobile Programming
PDF
No ratings yet
IOS Drawing Practical UIKit Solutions Mobile Programming
309 pages
UIKit
PDF
No ratings yet
UIKit
309 pages
The Greatest Css Tricks Vol I
PDF
100% (1)
The Greatest Css Tricks Vol I
103 pages
Web Programming Module 2 Important Topics PYQs
PDF
No ratings yet
Web Programming Module 2 Important Topics PYQs
38 pages
Learn: Beginner-To-Advanced
PDF
No ratings yet
Learn: Beginner-To-Advanced
98 pages
Iwt Unit-3
PDF
No ratings yet
Iwt Unit-3
59 pages
6-43 JS Adv
PDF
No ratings yet
6-43 JS Adv
43 pages
CSS Questions
PDF
No ratings yet
CSS Questions
90 pages
Css Final
PDF
No ratings yet
Css Final
16 pages
1 - Architecture Overview - Material Components For The Web
PDF
No ratings yet
1 - Architecture Overview - Material Components For The Web
11 pages
Cma Lab Programs 4th Sem
PDF
No ratings yet
Cma Lab Programs 4th Sem
32 pages
Svgpocketguide
PDF
No ratings yet
Svgpocketguide
92 pages
Experiment 4
PDF
No ratings yet
Experiment 4
6 pages
Unit 4
PDF
No ratings yet
Unit 4
40 pages
2 CSSNotes
PDF
No ratings yet
2 CSSNotes
102 pages
HTML5 SVG
PDF
No ratings yet
HTML5 SVG
17 pages
Unit 2 Web Design
PDF
No ratings yet
Unit 2 Web Design
43 pages
63be12efe4b0c16c0a4f740b Original
PDF
No ratings yet
63be12efe4b0c16c0a4f740b Original
22 pages
Multimedia Notes
PDF
No ratings yet
Multimedia Notes
11 pages
D3 Tips and Tricks by Malcolm Maclean
PDF
No ratings yet
D3 Tips and Tricks by Malcolm Maclean
367 pages
Cook P. Fundamentals of HTML, SVG, CSS and JavaScript For Data Visual. 2022
PDF
No ratings yet
Cook P. Fundamentals of HTML, SVG, CSS and JavaScript For Data Visual. 2022
87 pages
21H41A0429
PDF
No ratings yet
21H41A0429
18 pages
FullStackCafe QAS 1721545916351
PDF
No ratings yet
FullStackCafe QAS 1721545916351
7 pages
HTML Concepts
PDF
No ratings yet
HTML Concepts
242 pages
D3JS
PDF
No ratings yet
D3JS
31 pages
WDUnit 5
PDF
No ratings yet
WDUnit 5
28 pages
CSS Tricks - Modern and Advanced Techniques For 2022
PDF
No ratings yet
CSS Tricks - Modern and Advanced Techniques For 2022
20 pages
Corporate Training Syllabus
PDF
No ratings yet
Corporate Training Syllabus
44 pages
Bytes Hot Spec Summer Is BACK
PDF
No ratings yet
Bytes Hot Spec Summer Is BACK
10 pages
Unit Ii-1
PDF
No ratings yet
Unit Ii-1
40 pages
Unit 3-Q&A
PDF
No ratings yet
Unit 3-Q&A
11 pages
Computer Programming 4TH Quarter
PDF
No ratings yet
Computer Programming 4TH Quarter
8 pages
Colors and Backgrounds
PDF
No ratings yet
Colors and Backgrounds
41 pages
React+d3.js - Build Data Visualizations With React and d3.js
PDF
100% (2)
React+d3.js - Build Data Visualizations With React and d3.js
80 pages
HTML 5 Workshop
PDF
No ratings yet
HTML 5 Workshop
123 pages
HW4 Tutorial
PDF
No ratings yet
HW4 Tutorial
17 pages
D3 Tutorial: Introduction of Basic Components: HTML, CSS, SVG, and Javascript D3.Js Setup
PDF
No ratings yet
D3 Tutorial: Introduction of Basic Components: HTML, CSS, SVG, and Javascript D3.Js Setup
39 pages
Css Tips
PDF
No ratings yet
Css Tips
8 pages
HTML Canvas - Clock Tutorial PDF
PDF
No ratings yet
HTML Canvas - Clock Tutorial PDF
30 pages
CS474 M T: SVG - Animation Tutorial
PDF
No ratings yet
CS474 M T: SVG - Animation Tutorial
29 pages
Unit 3 Animation
PDF
No ratings yet
Unit 3 Animation
17 pages
Codeguppy Projects
PDF
No ratings yet
Codeguppy Projects
17 pages
LASICT Programming Q4 w504.25
PDF
No ratings yet
LASICT Programming Q4 w504.25
15 pages
Project Process Document
PDF
No ratings yet
Project Process Document
6 pages
CSS3
PDF
100% (1)
CSS3
29 pages
Wdtmodule-2 Parta
PDF
No ratings yet
Wdtmodule-2 Parta
7 pages
LM18
PDF
No ratings yet
LM18
8 pages
Drawing More Graphical Elements With SVG
PDF
No ratings yet
Drawing More Graphical Elements With SVG
34 pages
BookCars - Car Rental Platform With Mobile App
PDF
No ratings yet
BookCars - Car Rental Platform With Mobile App
48 pages
Unit 3
PDF
No ratings yet
Unit 3
8 pages
How To Use Jquery With Node - Js - GeeksforGeeks
PDF
No ratings yet
How To Use Jquery With Node - Js - GeeksforGeeks
1 page
(Micro Project) : (ACADEMIC YEAR:-2020-2021)
PDF
No ratings yet
(Micro Project) : (ACADEMIC YEAR:-2020-2021)
15 pages
HTML Graphics: Prepared by Shahzeb Khan
PDF
No ratings yet
HTML Graphics: Prepared by Shahzeb Khan
10 pages
Section Notes - Chat App Design
PDF
No ratings yet
Section Notes - Chat App Design
1 page
HTML5 Event Calendar - Scheduler
PDF
No ratings yet
HTML5 Event Calendar - Scheduler
30 pages
Untitled
PDF
No ratings yet
Untitled
19 pages
Data Visualization With D3.js. This Blog Deals With Visualization of - by Ankit Agarwal - Medium
PDF
No ratings yet
Data Visualization With D3.js. This Blog Deals With Visualization of - by Ankit Agarwal - Medium
1 page
25 Secrets of The Browser Developer Tools
PDF
No ratings yet
25 Secrets of The Browser Developer Tools
8 pages
App Settings Demystified (C# & VB)
PDF
No ratings yet
App Settings Demystified (C# & VB)
20 pages
Compare Two Excel Files
PDF
No ratings yet
Compare Two Excel Files
9 pages
Secure FTP or SFTP Using Putty-PSFTP With C# (Open Source) and Task Scheduler Configuration
PDF
No ratings yet
Secure FTP or SFTP Using Putty-PSFTP With C# (Open Source) and Task Scheduler Configuration
9 pages
d3 Cheat Sheet
PDF
No ratings yet
d3 Cheat Sheet
4 pages
100 Days of TypeScript (Day 7)
PDF
No ratings yet
100 Days of TypeScript (Day 7)
7 pages
Writing Custom Control With New WPF XAML Designer
PDF
No ratings yet
Writing Custom Control With New WPF XAML Designer
6 pages