| 1 |
(function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
var svgNS = 'http://www.w3.org/2000/svg'; |
| 5 |
|
| 6 |
function buildPath(opts) { |
| 7 |
var W = opts.svgWidth || 800; |
| 8 |
var H = opts.svgHeight || 200; |
| 9 |
var type = opts.pathType || 'arc'; |
| 10 |
|
| 11 |
if (type === 'arc') { |
| 12 |
var r = opts.arcRadius || 300; |
| 13 |
var cx = W / 2; |
| 14 |
var cy = opts.arcDirection === 'bottom' ? H - r - 20 : r + 20; |
| 15 |
var x1 = cx - r, x2 = cx + r; |
| 16 |
var sweep = opts.arcDirection === 'bottom' ? 0 : 1; |
| 17 |
return 'M ' + x1 + ' ' + cy + ' A ' + r + ' ' + r + ' 0 0 ' + sweep + ' ' + x2 + ' ' + cy; |
| 18 |
} |
| 19 |
|
| 20 |
if (type === 'wave') { |
| 21 |
var amp = opts.waveAmplitude || 40; |
| 22 |
var freq = opts.waveFrequency || 2; |
| 23 |
var midY = H / 2; |
| 24 |
var step = W / (freq * 2); |
| 25 |
var d = 'M 0 ' + midY; |
| 26 |
for (var i = 0; i < freq * 2; i++) { |
| 27 |
var cpX = step * (i + 0.5); |
| 28 |
var cpY = i % 2 === 0 ? midY - amp : midY + amp; |
| 29 |
var eX = step * (i + 1); |
| 30 |
var eY = midY; |
| 31 |
d += ' Q ' + cpX + ' ' + cpY + ' ' + eX + ' ' + eY; |
| 32 |
} |
| 33 |
return d; |
| 34 |
} |
| 35 |
|
| 36 |
if (type === 'circle') { |
| 37 |
var cr = Math.min(W, H) / 2 - 20; |
| 38 |
var ccx = W / 2, ccy = H / 2; |
| 39 |
return 'M ' + (ccx - cr) + ' ' + ccy + |
| 40 |
' A ' + cr + ' ' + cr + ' 0 1 1 ' + (ccx + cr) + ' ' + ccy + |
| 41 |
' A ' + cr + ' ' + cr + ' 0 1 1 ' + (ccx - cr) + ' ' + ccy; |
| 42 |
} |
| 43 |
|
| 44 |
if (type === 'scurve') { |
| 45 |
var midX2 = W / 2, sH = H; |
| 46 |
return 'M 0 ' + (sH * 0.7) + ' C ' + (midX2 * 0.5) + ' ' + (sH * 0.7) + ' ' + (midX2 * 0.5) + ' ' + (sH * 0.3) + ' ' + midX2 + ' ' + (sH * 0.5) + ' S ' + (midX2 + midX2 * 0.5) + ' ' + (sH * 0.8) + ' ' + W + ' ' + (sH * 0.3); |
| 47 |
} |
| 48 |
|
| 49 |
return 'M 0 ' + (H / 2) + ' L ' + W + ' ' + (H / 2); |
| 50 |
} |
| 51 |
|
| 52 |
document.querySelectorAll('.bkbg-tp-app').forEach(function (app) { |
| 53 |
var opts = {}; |
| 54 |
try { opts = JSON.parse(app.dataset.opts || '{}'); } catch (e) { } |
| 55 |
|
| 56 |
var W = opts.svgWidth || 800; |
| 57 |
var H = opts.svgHeight || 200; |
| 58 |
var text = opts.text || ''; |
| 59 |
var align2 = opts.align2 || 'center'; |
| 60 |
var sectionBg = opts.sectionBg || ''; |
| 61 |
|
| 62 |
if (sectionBg) app.style.background = sectionBg; |
| 63 |
|
| 64 |
var pathD = buildPath(opts); |
| 65 |
var pathId = 'bkbg-tp-path-' + Math.random().toString(36).slice(2, 7); |
| 66 |
|
| 67 |
// Create SVG |
| 68 |
var svg = document.createElementNS(svgNS, 'svg'); |
| 69 |
svg.setAttribute('viewBox', '0 0 ' + W + ' ' + H); |
| 70 |
svg.setAttribute('width', '100%'); |
| 71 |
svg.style.display = 'block'; |
| 72 |
svg.style.maxWidth = W + 'px'; |
| 73 |
svg.style.margin = align2 === 'center' ? '0 auto' : align2 === 'right' ? '0 0 0 auto' : '0'; |
| 74 |
svg.style.overflow = 'visible'; |
| 75 |
|
| 76 |
// defs |
| 77 |
var defs = document.createElementNS(svgNS, 'defs'); |
| 78 |
var pathDef = document.createElementNS(svgNS, 'path'); |
| 79 |
pathDef.setAttribute('id', pathId); |
| 80 |
pathDef.setAttribute('d', pathD); |
| 81 |
defs.appendChild(pathDef); |
| 82 |
svg.appendChild(defs); |
| 83 |
|
| 84 |
// Optional visible path stroke |
| 85 |
if (opts.showPath) { |
| 86 |
var pathVisible = document.createElementNS(svgNS, 'path'); |
| 87 |
pathVisible.setAttribute('d', pathD); |
| 88 |
pathVisible.setAttribute('fill', 'none'); |
| 89 |
pathVisible.setAttribute('stroke', opts.pathStrokeColor || '#e2e8f0'); |
| 90 |
pathVisible.setAttribute('stroke-width', opts.pathStrokeWidth || 1); |
| 91 |
svg.appendChild(pathVisible); |
| 92 |
} |
| 93 |
|
| 94 |
// Text |
| 95 |
var textEl = document.createElementNS(svgNS, 'text'); |
| 96 |
textEl.setAttribute('fill', opts.textColor || '#0f172a'); |
| 97 |
|
| 98 |
var textPath = document.createElementNS(svgNS, 'textPath'); |
| 99 |
textPath.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', '#' + pathId); |
| 100 |
textPath.setAttribute('href', '#' + pathId); |
| 101 |
textPath.setAttribute('startOffset', (opts.startOffset || 10) + '%'); |
| 102 |
textPath.setAttribute('textAnchor', opts.textAnchor || 'start'); |
| 103 |
textPath.textContent = text; |
| 104 |
|
| 105 |
// Animation: scroll startOffset |
| 106 |
var reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches; |
| 107 |
if (opts.animate && !reducedMotion) { |
| 108 |
var anim = document.createElementNS(svgNS, 'animate'); |
| 109 |
anim.setAttribute('attributeName', 'startOffset'); |
| 110 |
anim.setAttribute('from', '0%'); |
| 111 |
anim.setAttribute('to', '100%'); |
| 112 |
anim.setAttribute('dur', (opts.animateDuration || 12) + 's'); |
| 113 |
anim.setAttribute('repeatCount', opts.repeatCount || 'indefinite'); |
| 114 |
anim.setAttribute('calcMode', 'linear'); |
| 115 |
textPath.appendChild(anim); |
| 116 |
} |
| 117 |
|
| 118 |
textEl.appendChild(textPath); |
| 119 |
svg.appendChild(textEl); |
| 120 |
|
| 121 |
// Wrap the SVG |
| 122 |
var wrap = document.createElement('div'); |
| 123 |
wrap.className = 'bkbg-tp-wrap'; |
| 124 |
wrap.style.textAlign = align2; |
| 125 |
wrap.appendChild(svg); |
| 126 |
app.appendChild(wrap); |
| 127 |
}); |
| 128 |
})(); |
| 129 |
|