@@ -3,7 +3,7 @@ import { customElement, property } from 'lit/decorators.js';
33import { styleMap } from 'lit/directives/style-map.js' ;
44import { analog , ElementPin } from './pin' ;
55import { clamp } from './utils/clamp' ;
6- import { calculateBoundingRect } from './utils/geometry ' ;
6+ import { getScreenCTM } from './utils/ctm-workaround ' ;
77
88const knobCenter = {
99 x : 9.91 ,
@@ -193,42 +193,11 @@ export class PotentiometerElement extends LitElement {
193193
194194 private updateKnobMatrix ( ) {
195195 const knob = this . shadowRoot ?. querySelector < SVGRectElement > ( '#knob' ) ;
196- this . pageToKnobMatrix = knob ?. getScreenCTM ( ) ?. inverse ( ) ?? null ;
197-
198- const { userAgent } = navigator ;
199-
200- if ( userAgent . indexOf ( 'Firefox' ) >= 0 || userAgent . indexOf ( 'Epiphany' ) >= 0 ) {
201- // Firefox's getScreenCTM() is broken: https://bugzilla.mozilla.org/show_bug.cgi?id=1610093
202- const firefoxWorkaround =
203- this . shadowRoot ?. querySelector < SVGRectElement > ( '#firefox-workaround' ) ;
204- const boundingRect = firefoxWorkaround ?. getBoundingClientRect ( ) ;
205- const svgRect = firefoxWorkaround ?. ownerSVGElement ?. getBoundingClientRect ( ) ;
206- if ( ! boundingRect || ! svgRect ) {
207- return ;
208- }
209-
210- const cx = svgRect . x + svgRect . width / 2 ;
211- const cy = svgRect . y + svgRect . height / 2 ;
212- const deltaX = cx - ( boundingRect . x + boundingRect . width / 2 ) ;
213- const deltaY = cy - ( boundingRect . y + boundingRect . height / 2 ) ;
214- const angle = ( Math . atan2 ( deltaY , deltaX ) / Math . PI ) * 180 ;
215- const rotation = new DOMMatrix ( ) . rotate ( angle ) ;
216- const rotatedRect = calculateBoundingRect ( new DOMRect ( 0 , 9.5 , 1 , 1 ) , rotation ) ;
217- const sx = rotatedRect . width / boundingRect . width ;
218- const sy = rotatedRect . height / boundingRect . height ;
219- this . pageToKnobMatrix = rotation
220- . inverse ( )
221- . multiply (
222- new DOMMatrix ( [
223- sx ,
224- 0 ,
225- 0 ,
226- sy ,
227- rotatedRect . left - boundingRect . left * sx ,
228- rotatedRect . top - boundingRect . top * sy ,
229- ] )
230- ) ;
231- }
196+ const workaroundElement = this . shadowRoot ?. querySelector < SVGRectElement > ( '#firefox-workaround' ) ;
197+ this . pageToKnobMatrix =
198+ knob && workaroundElement
199+ ? getScreenCTM ( knob , workaroundElement , new DOMRect ( 0 , 9.5 , 1 , 1 ) )
200+ : null ;
232201 }
233202
234203 private rotateHandler ( event : MouseEvent | TouchEvent ) {
0 commit comments