UNPKG

@stringsync/vexml

Version:

MusicXML to Vexflow

36 lines (35 loc) 1.35 kB
import * as spatial from '../spatial'; const SCROLLER_HORIZONTAL_PADDING = 20; const SCROLLER_VERTICAL_PADDING = 20; export class Scroller { scrollContainer; constructor(scrollContainer) { this.scrollContainer = scrollContainer; } isFullyVisible(rect) { const visibleRect = this.getVisibleRect(); return (rect.x >= visibleRect.x && rect.y >= visibleRect.y && rect.x + rect.w <= visibleRect.x + visibleRect.w && rect.y + rect.h <= visibleRect.y + visibleRect.h); } scrollTo(position, behavior = 'auto') { if (!this.isAt(position)) { this.scrollContainer.scrollTo({ top: position.y - SCROLLER_VERTICAL_PADDING, left: position.x - SCROLLER_HORIZONTAL_PADDING, behavior, }); } } isAt(position) { return this.scrollContainer.scrollLeft === position.x && this.scrollContainer.scrollTop === position.y; } getVisibleRect() { const scrollLeft = this.scrollContainer.scrollLeft; const scrollTop = this.scrollContainer.scrollTop; const scrollWidth = this.scrollContainer.clientWidth; const scrollHeight = this.scrollContainer.clientHeight; return new spatial.Rect(scrollLeft, scrollTop, scrollWidth, scrollHeight); } }