From 4aaea8a6fe04b98d2ec67ca49c4c1655e58f2b60 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Sun, 28 Jun 2020 20:48:28 +0200 Subject: adding canvas, drawing tick timestamps based on zoom --- .../views/align/components/ticks.component.js | 83 ++++++++++++++++++++++ .../views/align/components/timeline.component.js | 75 +++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 animism-align/frontend/views/align/components/ticks.component.js create mode 100644 animism-align/frontend/views/align/components/timeline.component.js (limited to 'animism-align/frontend/views/align/components') diff --git a/animism-align/frontend/views/align/components/ticks.component.js b/animism-align/frontend/views/align/components/ticks.component.js new file mode 100644 index 0000000..3baa1f6 --- /dev/null +++ b/animism-align/frontend/views/align/components/ticks.component.js @@ -0,0 +1,83 @@ +import React, { Component } from 'react' + +import { ZOOM_STEPS, ZOOM_TICK_INTERVAL } from '../constants' +import { timestamp } from '../../../util' + +export default class Ticks extends Component { + render() { + let { start_ts, zoom, duration } = this.props.timeline + start_ts = 65.0 + duration /= 10 + zoom = 2 + const width = window.innerWidth + + let secondsPerPixel = ZOOM_STEPS[zoom] / 10 // 0.1 sec / step + let pixelTimeDuration = 1 / secondsPerPixel // secs per pixel + let widthTimeDuration = width / pixelTimeDuration // secs per pixel + console.log(secondsPerPixel, pixelTimeDuration) + console.log('width in seconds', widthTimeDuration) + + let secondsPerTick = ZOOM_STEPS[zoom] * 10 // secs + let pixelsPerTick = secondsPerTick * pixelTimeDuration + console.log('pixels per tick', pixelsPerTick) + + let labelSpacing = pixelsPerTick + let subdivision = secondsPerTick + while (labelSpacing < 200) { + labelSpacing *= 2 + subdivision *= 2 + } + + console.log('start ts', start_ts) + let pixelOffset = (start_ts / secondsPerPixel) + let pixelRemainder = pixelOffset % labelSpacing + // let startOffset = pixelsPerTick - (startTiming % pixelsPerTick) + let startOffset = labelSpacing - pixelRemainder + let startTiming = (pixelOffset + startOffset) * secondsPerPixel + + let tickCount = Math.ceil(width / labelSpacing) + let offset, timing, tickLabels = [], ticks = [] + for (var i = -1; i < tickCount; i++) { + offset = i * labelSpacing + startOffset + if (offset + 20 > width) continue + timing = i * subdivision + startTiming + if (timing > duration) { + break + } + tickLabels.push( +
+ {timestamp(timing)} +
+ ) + } + + // tickLabels.push( + //
+ // {timestamp(duration, 1)} + //
+ // ) + // for (var i = 0; i < minuteCount; i += 1) { + // offset = i * labelSpacing + // timing = i * subdivision + // ticks.push( + //
+ // ) + // } + return ( +
+ {ticks} + {tickLabels} +
+ ) + } +} diff --git a/animism-align/frontend/views/align/components/timeline.component.js b/animism-align/frontend/views/align/components/timeline.component.js new file mode 100644 index 0000000..af3c57b --- /dev/null +++ b/animism-align/frontend/views/align/components/timeline.component.js @@ -0,0 +1,75 @@ +import React, { Component } from 'react' +// import { Link } from 'react-router-dom' +// import { bindActionCreators } from 'redux' +import { connect } from 'react-redux' + +import actions from '../../../actions' +// import * as uploadActions from './upload.actions' + +import Ticks from './ticks.component' + +import * as constants from '../constants' + +class Timeline extends Component { + constructor(props){ + super(props) + this.canvasRef = React.createRef() + this.handleKeydown = this.handleKeydown.bind(this) + } + componentDidMount() { + this.bind() + this.resize() + this.draw() + } + componentDidUpdate() { + this.draw() + } + componentWillUnmount() { + this.unbind() + } + bind() { + document.addEventListener('keydown', this.handleKeydown) + } + unbind() { + document.removeEventListener('keydown', this.handleKeydown) + } + handleKeydown(e) { + // console.log(e.shiftKey, e.keyCode) + } + resize() { + const canvas = this.canvasRef.current + canvas.width = window.innerWidth + canvas.height = constants.DEFAULT_HEIGHT + } + draw() { + const canvas = this.canvasRef.current + const ctx = canvas.getContext('2d') + const w = window.innerWidth + this.clearCanvas(ctx, w) + } + clearCanvas(ctx, w) { + const h = constants.WAVEFORM_HEIGHT + ctx.clearRect(0, 0, w, h) + ctx.fillStyle = 'rgba(0,0,0,0.5)' + ctx.fillRect(0, 0, w, h) + ctx.fillStyle = 'rgba(64,128,192,0.5)' + } + render() { + return ( +
+ + +
+ ) + } +} + +const mapStateToProps = state => ({ + timeline: state.align.timeline, +}) + +const mapDispatchToProps = dispatch => ({ + // uploadActions: bindActionCreators({ ...uploadActions }, dispatch), +}) + +export default connect(mapStateToProps, mapDispatchToProps)(Timeline) -- cgit v1.2.3-70-g09d2