-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwidget-qrcode.js
187 lines (184 loc) · 5.75 KB
/
widget-qrcode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import QRCode from './module/qrcode.js';
import Draw from './module/draw.js';
import styleSheet from './style/default.css' assert { type: 'css'};
class WidgetQRCode extends HTMLElement {
constructor() {
super();
this.attachShadow({mode:'open'});
}
static get observedAttributes(){
return ['value','template','level','width','height','logo','text','text-color','text-stroke','foreground-image','background-image','foreground-color','background-color','inner-color','outer-color'];
}
get value(){
return this.getAttribute('value')||'https://passer-by.com/';
}
set value(value){
return this.setAttribute('value',value);
}
get template(){
return this.getAttribute('template')||'default';
}
set template(value){
return this.setAttribute('template',value);
}
get level(){
return this.getAttribute('level')||'H';
}
set level(value){
return this.setAttribute('level',value);
}
get width(){
return +this.getAttribute('width')||0;
}
set width(value){
return this.setAttribute('width',value);
}
get height(){
return +this.getAttribute('height')||0;
}
set height(value){
return this.setAttribute('height',value);
}
get logo(){
return this.getAttribute('logo')||'';
}
set logo(value){
return this.setAttribute('logo',value);
}
get text(){
return this.getAttribute('text')||'';
}
set text(value){
return this.setAttribute('text',value);
}
get textColor(){
return this.getAttribute('text-color')||'';
}
set textColor(value){
return this.setAttribute('text-color',value);
}
get textStroke(){
return this.getAttribute('text-stroke')||'';
}
set textStroke(value){
return this.setAttribute('text-stroke',value);
}
get foregroundImage(){
return this.getAttribute('foreground-image')||'';
}
set foregroundImage(value){
return this.setAttribute('foreground-image',value);
}
get backgroundImage(){
return this.getAttribute('background-image')||'';
}
set backgroundImage(value){
return this.setAttribute('background-image',value);
}
get foregroundColor(){
return this.getAttribute('foreground-color')||'';
}
set foregroundColor(value){
return this.setAttribute('foreground-color',value);
}
get backgroundColor(){
return this.getAttribute('background-color')||'';
}
set backgroundColor(value){
return this.setAttribute('background-color',value);
}
get innerColor(){
return this.getAttribute('inner-color')||'';
}
set innerColor(value){
return this.setAttribute('inner-color',value);
}
get outerColor(){
return this.getAttribute('outer-color')||'';
}
set outerColor(value){
return this.setAttribute('outer-color',value);
}
attributeChangedCallback(name, oldValue, newValue){
if(oldValue!=newValue){
this.context&&this.drawQRCode();
}
}
connectedCallback () {
let _ = this;
// 模板
const defaultSheet = new CSSStyleSheet();
defaultSheet.insertRule(`:host{
width: ${this.width||300}px;
height: ${this.height||300}px;
}`);
if(_.shadowRoot.adoptedStyleSheets){
_.shadowRoot.adoptedStyleSheets = [defaultSheet,styleSheet];
}else{
const $style = document.createElement('style');
$style.rel = 'stylesheet';
$style.textContent = [defaultSheet.cssRules,...styleSheet.cssRules].map(item=>item.cssText).join('');
_.shadowRoot.appendChild($style);
}
// 节点
_.render();
_.drawQRCode();
// 自适应
_.addEventListener('resize',function(){
_.resize();
},false);
// Edge浏览器切换tab画布被清空问题
if(navigator.userAgent.includes('Edg/')){
document.addEventListener('visibilitychange',function(){
_.drawQRCode();
});
}
}
render(parser){
let _ = this;
_.shadowRoot.innerHTML = `<div class="mod-qrcode">
<canvas></canvas>
</div>`;
_.$module = _.shadowRoot.querySelector('.mod-qrcode');
_.$canvas = _.$module.querySelector('canvas');
_.context = _.$canvas.getContext('2d');
_.resize();
if (typeof ResizeObserver !== 'undefined') {
let observer = new ResizeObserver(() => {
_.resize();
});
observer.observe(_);
}
}
resize(){
let _ = this;
let style = window.getComputedStyle(_);
if(style.width&&style.height){
let clientSize = Math.max(parseInt(style.width),parseInt(style.height));
_.$canvas.width = clientSize*2;
_.$canvas.height = clientSize*2;
_.drawQRCode();
}
}
drawQRCode(){
let _ = this;
let level =_.logo?'H':_.level;
let data = QRCode(_.value, level);
_.context.clearRect(0,0,_.$canvas.width,_.$canvas.height);
(Draw[_.template]||Draw['default'])(_.context, data, {
'foregroundImage':_.foregroundImage,
'backgroundImage':_.backgroundImage,
'foregroundColor':_.foregroundColor,
'backgroundColor':_.backgroundColor,
'innerColor':_.innerColor,
'outerColor':_.outerColor,
'logo':_.logo,
'text':_.text,
'textColor':_.textColor,
'textStroke':_.textStroke
});
}
}
if(!customElements.get('widget-qrcode')){
customElements.define('widget-qrcode', WidgetQRCode);
}