All files / src/components/music/sidebar InstrumentEnvelopePreview.tsx

0% Statements 0/80
0% Branches 0/45
0% Functions 0/2
0% Lines 0/78

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                                                 
import React, { useContext, useEffect, useRef } from "react";
import { ThemeContext } from "styled-components";
 
interface InstrumentEnvelopePreviewProps {
  volume: number;
  sweep: number;
  length: number | null;
}
 
const PADDING = 10;
 
export const InstrumentEnvelopePreview = ({
  volume,
  sweep,
  length,
}: InstrumentEnvelopePreviewProps) => {
  const canvasRef = useRef<HTMLCanvasElement>(null);
  const themeContext = useContext(ThemeContext);
 
  useEffect(() => {
    const canvas = canvasRef.current;
    Iif (!canvas || !themeContext) {
      return;
    }
 
    const ctx = canvas.getContext("2d");
    Iif (!ctx) {
      return;
    }
 
    const rect = canvas.getBoundingClientRect();
    const dpr = window.devicePixelRatio || 1;
 
    const width = rect.width;
    const height = rect.height;
 
    const pixelWidth = Math.round(width * dpr);
    const pixelHeight = Math.round(height * dpr);
 
    Iif (canvas.width !== pixelWidth || canvas.height !== pixelHeight) {
      canvas.width = pixelWidth;
      canvas.height = pixelHeight;
    }
 
    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
 
    const drawWidth = width - PADDING * 2;
    const drawHeight = height - PADDING * 2;
 
    const normalisedVolume = volume / 15;
    const secLength = length === null ? 1 : length / 256;
    const centerLineY = height - PADDING - (7 / 15) * drawHeight;
 
    const defaultColor = themeContext?.colors.tracker.wave ?? "#fff";
    const backgroundColor =
      themeContext?.colors.tracker.waveBackground ?? "#000";
    const gridColor = themeContext?.colors.tracker.waveGrid ?? "#333";
 
    ctx.fillStyle = backgroundColor;
    ctx.fillRect(0, 0, width, height);
 
    // center line
    ctx.beginPath();
    ctx.strokeStyle = gridColor;
    ctx.lineWidth = 1;
    ctx.moveTo(0, Math.round(centerLineY) + 0.5);
    ctx.lineTo(width, Math.round(centerLineY) + 0.5);
    ctx.stroke();
 
    ctx.lineWidth = 1;
    ctx.lineCap = "round";
 
    ctx.shadowColor = defaultColor;
    ctx.shadowBlur = 5;
 
    ctx.beginPath();
    ctx.moveTo(PADDING, height - PADDING - normalisedVolume * drawHeight);
 
    let localsweep;
 
    if (sweep < 0) {
      // fade down
      localsweep = sweep + 8;
      const envLength = ((localsweep / 64) * volume) / 2;
      const progress = envLength > 0 ? Math.min(secLength / envLength, 1) : 1;
      const currentVolume = volume * (1 - progress);
      const currentY = height - PADDING - (currentVolume / 15) * drawHeight;
 
      ctx.lineTo(
        PADDING + Math.min(envLength, secLength) * drawWidth,
        currentY,
      );
 
      ctx.lineTo(PADDING + secLength * drawWidth, height - PADDING);
    } else if (sweep > 0) {
      // fade up
      localsweep = 8 - sweep;
      const envLength = ((localsweep / 64) * (15 - volume)) / 2;
      const progress = envLength > 0 ? Math.min(secLength / envLength, 1) : 1;
      const currentVolume = volume + (15 - volume) * progress;
      const currentY = height - PADDING - (currentVolume / 15) * drawHeight;
 
      ctx.lineTo(
        PADDING + Math.min(envLength, secLength) * drawWidth,
        currentY,
      );
 
      ctx.lineTo(PADDING + secLength * drawWidth, currentY);
    } else {
      // no fade
      ctx.lineTo(
        PADDING + secLength * drawWidth,
        height - PADDING - normalisedVolume * drawHeight,
      );
    }
 
    Iif (secLength !== 1) {
      ctx.lineTo(PADDING + secLength * drawWidth, height - PADDING);
      Iif (secLength < 1) {
        ctx.lineTo(PADDING + drawWidth, height - PADDING);
      }
    }
 
    ctx.shadowColor = defaultColor;
    ctx.lineWidth = 1;
    ctx.strokeStyle = "black";
 
    for (let i = 0; i < 10; i++) {
      ctx.stroke();
      ctx.shadowBlur = i * 6;
    }
 
    ctx.lineWidth = 2;
    ctx.lineCap = "round";
    ctx.shadowBlur = 0;
    ctx.strokeStyle = defaultColor;
    ctx.stroke();
  }, [volume, length, themeContext, sweep]);
 
  return (
    <canvas
      ref={canvasRef}
      style={{
        width: "100%",
        height: "100px",
        backgroundColor: "#000",
        borderRadius: 4,
        display: "block",
      }}
    />
  );
};