UNPKG

favicon-badge-notify

Version:
2 lines (1 loc) 952 B
(function(o,d){typeof exports=="object"&&typeof module<"u"?module.exports=d():typeof define=="function"&&define.amd?define(d):(o=typeof globalThis<"u"?globalThis:o||self,o.index=d())})(this,function(){"use strict";return({src:d,backgroundColor:l="red",textColor:c="white"})=>{let n=96;const i=(document==null?void 0:document.createElement("canvas"))||{};i.width=n,i.height=n;const e=i==null?void 0:i.getContext("2d"),t=document==null?void 0:document.createElement("img");let s=()=>{};const f=a=>{e.drawImage(t,0,0,n,n),e.beginPath(),e.arc(i.width-n/3,n/3,n/3,0,2*Math.PI),e.fillStyle=l,e.fill(),e.font='55px "helvetica", sans-serif',e.textAlign="center",e.textBaseline="middle",e.fillStyle=c,e.fillText(a,i.width-n/3,n/3)};return{drawBadge:a=>(t==null||t.setAttribute("src",d),new Promise(r=>{s=()=>{a&&e?(f(a),r(i.toDataURL("image/png"))):r(d)},t==null||t.addEventListener("load",s)})),destroyBadge:()=>{t==null||t.removeEventListener("load",s)}}}});