Skip to content

Commit

Permalink
Start writing the renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
luxluth committed Jul 10, 2023
1 parent 810cda5 commit dfbff92
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parse } from 'ass-compiler'
import { Renderer } from './renderer'
import { ASSOptions } from './types'
import { newCanvas } from './utils'

Expand All @@ -7,6 +8,7 @@ export default class ASS {
video: HTMLVideoElement | string
videoElement: HTMLVideoElement | null = null
canvas: HTMLCanvasElement| null = null
renderer: Renderer | null = null
constructor(
options: ASSOptions
) {
Expand All @@ -23,21 +25,23 @@ export default class ASS {
} else {
this.videoElement = this.video
}
console.log(this.videoElement)

this.setCanvasSize()

this.renderer = new Renderer(parse(this.assText), this.canvas as HTMLCanvasElement)
this.videoElement?.addEventListener('loadedmetadata', () => {
this.setCanvasSize()
})

window.addEventListener('resize', () => {
this.setCanvasSize();
this.renderer?.redraw()
});

this.renderer.render()
}


setCanvasSize() {
console.log("set Canva size")
const { videoWidth, videoHeight, offsetTop, offsetLeft } = this.videoElement as HTMLVideoElement
const aspectRatio = videoWidth / videoHeight;

Expand Down
20 changes: 20 additions & 0 deletions src/renderer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { ParsedASS } from "ass-compiler";
export class Renderer {
parsedAss: ParsedASS
canvas: HTMLCanvasElement
ctx: CanvasRenderingContext2D
constructor(parsedASS: ParsedASS, canvas: HTMLCanvasElement) {
this.parsedAss = parsedASS
this.canvas = canvas
this.ctx = canvas.getContext("2d") as CanvasRenderingContext2D
if (this.ctx === null) { throw new Error("Unable to initilize the Canvas 2D context") }
let data = [
{parsedAss : this.parsedAss},
{canvas : this.canvas},
{ctx : this.ctx}
]
console.debug(data)
}
render() {}
redraw() {}
}

0 comments on commit dfbff92

Please sign in to comment.