1 function writeBottomCircle(text, canvaId, diameter) {
2 var canvas = document.getElementById(canvaId);
16 function writeTopCircle(text, canvaId, diameter) {
17 var canvas = document.getElementById(canvaId);
31 function writeCircularText(
42 // declare and intialize canvas, reference, and useful variables
43 align = align.toLowerCase();
44 var ctxRef = mainCanvas.getContext('2d');
45 // ctx.resetTransform();
46 var clockwise = align == 'right' ? 1 : -1; // draw clockwise for aligned right. Else Anticlockwise
47 startAngle = startAngle * (Math.PI / 180); // convert to radians
49 // calculate height of the font. Many ways to do this
50 // you can replace with your own!
53 // set text attributes
54 ctxRef.font = `${fontSize}px ${fontFamily}`;
55 ctxRef.fillStyle = 'white';
57 // Reverse letters for align Left inward, align right outward
58 // and align center inward.
60 (['left', 'center'].indexOf(align) > -1 && inwardFacing) ||
61 (align == 'right' && !inwardFacing)
63 text = text.split('').reverse().join('');
65 // Setup letters and positioning
66 ctxRef.translate(diameter / 2 - 5, diameter / 2); // Move to center
67 startAngle += Math.PI * !inwardFacing; // Rotate 180 if outward
68 ctxRef.textBaseline = 'middle'; // Ensure we draw in exact center
69 ctxRef.textAlign = 'center'; // Ensure we draw in exact center
71 // rotate 50% of total angle for center alignment
72 if (align == 'center') {
73 for (var j = 0; j < text.length; j++) {
74 var charWid = ctxRef.measureText(text[j]).width;
76 ((charWid + (j == text.length - 1 ? 0 : kerning)) /
77 (diameter / 2 - textHeight) /
83 // Phew... now rotate into final start position
84 ctxRef.rotate(startAngle);
86 // Now for the fun bit: draw, rotate, and repeat
87 for (var j = 0; j < text.length; j++) {
88 var charWid = ctxRef.measureText(text[j]).width; // half letter
90 ctxRef.rotate((charWid / 2 / (diameter / 2 - textHeight)) * clockwise);
91 // draw the character at "top" or "bottom"
92 // depending on inward or outward facing
97 (inwardFacing ? 1 : -1) * (0 - diameter / 2 + textHeight / 2)
101 ((charWid / 2 + kerning) / (diameter / 2 - textHeight)) * clockwise
102 ); // rotate half letter
104 ctxRef.resetTransform();