-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* there are four fields - Occlusion -> for cloze - Image - Header - Back Extra * the implementation for shape generation to canvas added reviewer ts
- Loading branch information
Showing
9 changed files
with
168 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright: Ankitects Pty Ltd and contributors | ||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html | ||
|
||
export function setupImageCloze() { | ||
const canvas: HTMLCanvasElement = document.createElement("CANVAS") as HTMLCanvasElement; | ||
canvas.id = "image-occlusion-canvas"; | ||
canvas.style.backgroundSize = "100% 100%"; | ||
canvas.style.maxWidth = "100%"; | ||
canvas.style.maxHeight = "90vh"; | ||
|
||
const ctx: CanvasRenderingContext2D = canvas.getContext("2d")!; | ||
const imageFieldElem = document.getElementById("img") as HTMLImageElement; | ||
imageFieldElem.style.display = "none"; | ||
|
||
const img: HTMLImageElement = new Image(); | ||
img.src = imageFieldElem.src; | ||
|
||
img.onload = () => { | ||
canvas.width = img.width; | ||
canvas.height = img.height; | ||
ctx.drawImage(img, 0, 0, img.width, img.height); | ||
drawShapes(ctx); | ||
}; | ||
|
||
document.getElementById("qa")!.appendChild(canvas); | ||
} | ||
|
||
function drawShapes(ctx: CanvasRenderingContext2D) { | ||
const activeCloze = document.querySelectorAll(".cloze"); | ||
const inActiveCloze = document.querySelectorAll(".cloze-inactive"); | ||
|
||
for (let clz of activeCloze) { | ||
let cloze = (<HTMLDivElement>clz); | ||
const shape = cloze.dataset.shape!; | ||
const fill = cloze.dataset.quesmaskcolor!; | ||
draw(ctx, cloze, shape, fill); | ||
} | ||
|
||
for (let clz of inActiveCloze) { | ||
let cloze = (<HTMLDivElement>clz); | ||
const shape = cloze.dataset.shape!; | ||
const fill = cloze.dataset.fill!; | ||
draw(ctx, cloze, shape, fill); | ||
} | ||
} | ||
|
||
function draw(ctx: CanvasRenderingContext2D, cloze: HTMLDivElement, shape: string, color: string) { | ||
ctx.fillStyle = color; | ||
|
||
const post_left = parseFloat(cloze.dataset.left!); | ||
const pos_top = parseFloat(cloze.dataset.top!); | ||
const width = parseFloat(cloze.dataset.width!); | ||
const height = parseFloat(cloze.dataset.height!); | ||
|
||
switch (shape) { | ||
case "rect": | ||
ctx.fillRect(post_left, pos_top, width, height); | ||
break; | ||
|
||
case "ellipse": | ||
const rx = parseFloat(cloze.dataset.rx!); | ||
const ry = parseFloat(cloze.dataset.ry!); | ||
ctx.beginPath(); | ||
ctx.ellipse(post_left, pos_top, rx, ry, 0, 0, Math.PI * 2, false); | ||
ctx.fill(); | ||
break; | ||
|
||
case "polygon": | ||
const points = JSON.parse(cloze.dataset.points!); | ||
ctx.beginPath(); | ||
ctx.moveTo(points[0][0], points[0][1]); | ||
for (let i = 1; i < points.length; i++) { | ||
ctx.lineTo(points[i][0], points[i][1]); | ||
} | ||
ctx.closePath(); | ||
ctx.fill(); | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters