• Stars
    star
    195
  • Rank 199,374 (Top 4 %)
  • Language
  • License
    MIT License
  • Created almost 7 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Here it is, an SVG calendar which always display's today's date.

This SVG always shows today's date

Originally posted on my blog.

For my contact page, I wanted a generic calendar icon to let people view my diary. Calendar icons are almost always a skeuomorph of a paper calendar, but I wondered if I could make it slightly more useful by creating a dynamic icon.

Here it is, an SVG calendar which always display's today's date.

(Note - GitHub doesn't allow data URIs or iFrames - so you'll need to click the link to see it)

The background image is derived from the Twitter TweMoji Calendar icon - CC-BY.

Buy me a coffee

Use

SVG supports JavaScript. Browsers will only run the JavaScript if the SVG is included...

  • in an iframe <iframe src="calendar.svg"></iframe>
  • as inline <body><svg>.....</svg></body>

It will not run JavaScript in an <img> element.

HOWTO

Text support in SVG is a little awkward, so let me explain how I did this.

SVG supports JavaScript. This will run as soon as the image is loaded.

<svg onload="init(evt)" xmlns="http://www.w3.org/2000/svg"
aria-label="Calendar" role="img" viewBox="0 0 512 512">

Next step is to get the various date strings. I'm using the en-GB locale as that's where I'm based.

<script type="text/ecmascript"><![CDATA[
function init(evt) {
  var time = new Date();
  var locale = "en-gb";

I want to display something like "Sunday 25 FEB" - the locale options allow for short and long names. So you could have "SUN 25 February".

  var DD   = time.getDate();
  var DDDD = time.toLocaleString(locale, {weekday: "long"});
  var MMM = time.toLocaleString(locale,  {month:   "short"});

Finally, we need to add the text on to the image.

  var svgDocument = evt.target.ownerDocument;

  var dayNode = svgDocument.createTextNode(DD);
  svgDocument.getElementById("day").appendChild(dayNode);

  var weekdayNode = svgDocument.createTextNode(DDDD);
  svgDocument.getElementById("weekday").appendChild(weekdayNode);

  var monthNode = svgDocument.createTextNode(MMM.toUpperCase());
  svgDocument.getElementById("month").appendChild(monthNode);
  
}
]]></script>

Text positioning is relatively simplistic. An X & Y position which is anchored to the bottom of the text - remember that letters with descenders like g will extend beyond the bottom of the Y co-ordinate. This is also where we set the colour of the text, its size, and a font.

A monospace font makes it easier to predict the layout.

<text id="month"
  x="32" 
  y="164" 
  fill="#fff" 
  font-family="monospace"
  font-size="140px"
  style="text-anchor: left"></text>

A word on anchoring. To centre the anchor, use style="text-anchor: middle"

A quick test shows that this works on all desktop browsers and Android browsers. I've not tested on iPhones or anything more exotic.

Enjoy!

More Repositories

1

SuperTinyIcons

Under 1KB each! Super Tiny Icons are miniscule SVG versions of your favourite website and app logos
HTML
14,675
star
2

BMW-OpenSource

All the Open Source Software provided by BMW for their i3
C
591
star
3

BMW-i-Remote

A reverse engineered interface for the BMW i3 Electric Car
Python
389
star
4

Sercomm-API

A comprehensive guide to the controlling Sercomm IP Cameras via their inbuit API
178
star
5

Tweet2Embed

Convert a public Tweet into embedded semantic HTML
Python
156
star
6

TweeView

A Tree View For Tweets
JavaScript
94
star
7

Renault-Zoe-API

A basic API for getting information about your Renault Zoe Electric Vehicle.
Python
93
star
8

SVGtestcard

Monitor Calibration Test Cards in SVG - loosely based on the BBC's HD Test Card X
73
star
9

QR-Generator-PHP

QR generator. Supports JPG, GIF, PNG outputs. Image size <=1480px. Error correction. Downloadable images (via content disposition)
PHP
71
star
10

Twitter-Networks

Create a directed network of Twitter followers.
Python
70
star
11

Tate-Hack

A collection of random things for #hackthespace
Python
63
star
12

Extended-Hyperlegible

An updated version of the Atkinson Hyperlegible font from https://brailleinstitute.org/freefont
54
star
13

PythonPals

The Python Pals are "Solve Your Own Adventure" stories to help teach people Python.
43
star
14

RaSMSpberry-Pi

Collection of tools to enable the Raspberry Pi to send SMS via an attached 3G USB dongle
Python
42
star
15

3D-Screenshot-to-3D-Model

Quick script to transform screenshots of 3D movies into a 3D meshes
Python
34
star
16

Open-Source-Shakespeare

A MySQL database of the complete works of William Shakespeare
31
star
17

edent.tel

A semantic contact page built around SVG
HTML
28
star
18

Python-Twitter-Hacks

Being mostly a collection of code snippets to remind me how to do things.
Python
27
star
19

PirateMuseum

A virtual museum of stolen artworks
JavaScript
25
star
20

Amazon-Wishlist-Pricedrop-Alert

Emails you when items on your wishlist drop in price
Python
19
star
21

location-activitypub-symfony

A basic ActivityPub server for sharing your location. Written in PHP / Symfony
JavaScript
19
star
22

Fronius-DataManager-Solar-Logger

Log the performance of your solar panels with Fronius's DataManager
PHP
17
star
23

RGB_Colours

This is the code which powers the Twitter Bot https://twitter.com/RGB_Colours
Python
15
star
24

Shakespeare-Serif-Font

A serif font based on Shakespeare's First Folio.
Python
13
star
25

HyperThread

A way to follow complex conversation threads using a HyperTree
JavaScript
10
star
26

Dabr-For-AppDotNet

A first attempt to get Dabr to use the AppDotNet API
PHP
8
star
27

motion-chart

Create a "Hans Rosling" style motion chart using D3.
6
star
28

aed

A Mobile Web App to find Emergency Defibrillators in South & Central UK
6
star
29

blog-theme

A simple, lightweight, easy to maintain WordPress theme for https://shkspr.mobi/blog/
PHP
5
star
30

South-Up-Aotearoa-Centred-Equal-Earth-Projection-Map

South Up, Aotearoa Centred, Equal-Earth Projection Map
R
4
star
31

WordPress-Calendar-Widget

A simple widget to display a blog's post history as a calendar.
PHP
4
star
32

AvatarPreview

A quick way to preview square and circular avatar images
HTML
3
star
33

Applicationize

A personal collection of Chrome WebApps created using https://applicationize.me/
3
star
34

SuperHub

Virgin Media SuperHub2 and 2ac monitoring scripts.
Python
3
star
35

aframedemo

Demo for GameDevDay.club
HTML
2
star
36

w3c-voice-assistant-cg

Exploration and discussion of standards for voice assistants.
2
star
37

Harry-Potter-Lifx

Control your Lifx bulbs by saying "Lumos" for on, and "Knox" for off. A disgustingly quick and dirty hack. Only works in Chrome.
HTML
2
star
38

Mobile-Web-Torrent

A mobile friendly website for searching for torrents - searching is done by the handset, not the server.
2
star
39

edent

It's me!
1
star
40

WordPress-On-This-Day-Plugin

A plugin to show blog posts published "on this day"
PHP
1
star
41

ThinkingCleaner-WiFi-Roomba-Python

A set of Python tools for interacting with a WiFi Enabled Roomba.
Python
1
star
42

3D-UK-Money

OpenSCAD and STL files to 3D print UK coins. Includes £2, £1, 50p, 20p, 10p, 5p, 1p.
1
star
43

FloodForecast

Tweeting the Flood Forecast for England and Wales. A BlueLightCamp Hack
Python
1
star
44

AAL

AAL is a Software License which can be added to any Open Source or Closed Source software.
1
star
45

Sudoku-Twitter-Bot-Python

A simple bot for generating Sudoku puzzles and posting them to Twitter.
Python
1
star