MCPcopy Create free account
hub / github.com/firebase/functions-samples / clock

Function clock

Node-1st-gen/image-maker/functions/clock.js:41–131  ·  view source on GitHub ↗
(ctx, colorOpts)

Source from the content-addressed store, hash-verified

39};
40
41const clock = (ctx, colorOpts) => {
42 const colors = _.merge({}, getDefaultOpts(), colorOpts);
43 let x, y, i;
44 const now = new Date();
45
46 ctx.clearRect(0, 0, 320, 320);
47 ctx.save();
48
49 // Clock background
50 ctx.translate(160, 160);
51 ctx.beginPath();
52 ctx.lineWidth = 14;
53 ctx.strokeStyle = colors.strokes.clock;
54 ctx.fillStyle = colors.fills.clock;
55 ctx.arc(0, 0, 142, 0, Math.PI * 2, true);
56 ctx.stroke();
57 ctx.fill();
58
59 // Hour marks
60 ctx.lineWidth = 8;
61 ctx.strokeStyle = colors.strokes.hour;
62 for (i = 0; i < 12; i++) {
63 x = getX(Math.PI / 6 * i);
64 y = getY(Math.PI / 6 * i);
65 ctx.beginPath();
66 ctx.moveTo(x * 100, y * 100);
67 ctx.lineTo(x * 125, y * 125);
68 ctx.stroke();
69 }
70
71 // Minute marks
72 ctx.lineWidth = 5;
73 ctx.strokeStyle = colors.strokes.minute;
74 for (i = 0; i < 60; i++) {
75 if (i % 5 !== 0) {
76 x = getX(Math.PI / 30 * i);
77 y = getY(Math.PI / 30 * i);
78 ctx.beginPath();
79 ctx.moveTo(x * 117, y * 117);
80 ctx.lineTo(x * 125, y * 125);
81 ctx.stroke();
82 }
83 }
84
85 const sec = now.getSeconds();
86 const min = now.getMinutes();
87 const hr = now.getHours() % 12;
88
89 ctx.fillStyle = 'black';
90
91 // Write hours
92 x = getX(hr * (Math.PI / 6) + (Math.PI / 360) * min + (Math.PI / 21600) * sec);
93 y = getY(hr * (Math.PI / 6) + (Math.PI / 360) * min + (Math.PI / 21600) * sec);
94 ctx.lineWidth = 14;
95 ctx.beginPath();
96 ctx.moveTo(x * -20, y * -20);
97 ctx.lineTo(x * 80, y * 80);
98 ctx.stroke();

Callers 1

index.jsFile · 0.85

Calls 3

getDefaultOptsFunction · 0.85
getXFunction · 0.85
getYFunction · 0.85

Tested by

no test coverage detected