Feature image occlusion (#2367)
* add note types with occlusions and image fields
* generate image occlusion cloze div data
- generate div element with data-* atrributes for canvas shape generate for reviewer
* getting image data & deck id and adding notes
the implementation added into backend
- added service index in backend.proto for image occlusion request
- created image_occlusion.proto with required message and service
- implementation in backend for getting image and adding notes, also during editing return imagecloze note and update notes
- add notes to selected deck, if no notetype then add image occlusion notetypes
- reuse notetype from stock notetypes when not exist
* script for generating shapes using canvas api in reviewer
- the flash issues fixed by loading image and using image size to draw canvas, also when image get resized, calculate scale using natural width and canvas width to draw shape at right position
- limit size of canvas for safari
* init image occlusion page in ts and build page
with
- fabricjs for editing shapes
- panzoom for drag and zoom
- pickr for color picker
- build page using web.rs
* implement top toolbar for canvas shapes
- undo & redo tools
- zoom in, zoom out and zoom fit
- group & ungroup
- copy & paste
- set transparency of shapes
- align tools
* implement side toolbar for drawing shapes
add top toolbar and the side toolbar contains following tools
- cursor for selecting shapes
- zoom for drag and zoom shapes in mask editor
- rectangle for creating it
- ellipse for creating it
- polygon for creating it using points
- shape fill color
- question mask color (currently only single color can be added for all shapes)
* add maskeditor page for editing mask
- add side toolbar and sidebar include toptoolbar
- load maskeditor in two mode
- for adding note using path to image
- for editing note using note id
* implement note editor page for adding notes
- the note editor page have simple button (B/I/U) and option to toggle html view
- option to select deck for adding notes into that deck
- option to generate to hide all, guess one & hide one, guess one notes
* add image occlusion page
add side toolbar, top toolbar, mask editor and note editor
- option to switch between mask editor and note editor
* implement generates notes and save notes
implemention to show toast components for messages
* removed pickr & implemented color picker component
- remove pickr
- implemented using html5 canvas
- range input for changing color
- another range input for opacity changes
- hex and rgba value support
* rename methods name & rust unwrap safety
- change plural names to singular
- create respone message in proto and return response with imagecloze note or error if not found with note id
- remove image_occlusion from post handler list
- rename service name in mediasrv.py
- rename methods name for image occlusion in backend and image_occlusion
- update frontend also for update functions' names
- handle error in frontend mask-editor.ts, when error getting notes then toast message shown to frontend
* extract to function & add comments & remove global
- extract function in mask-editor.ts to reduce duplicate
- remove unused global from css
- add comments to store.ts explaining usage
- changes id to noteId in lib.ts
- add comments for limitSize, becuase of duplicate implementation
* remove image_occlusion notetype
- remove from stock notetype, stdmodels
- add implementation for notetype to image occlusion
- add i18n for errors
* update smooth scroll, always show cursor tools
- change questionmask to qmask
- make selectable for shape true in all tools to simplify edits and draw shapes
- update image occlusion in reviewer ts to load image properly
* add and get notetype else return errors
* fix: not showing occlusion
* Use a oneof for ImageClozeNoteResponse
Makes it clearer that only one of them can be returned
* Don't crash if image filename not provided
The second unwrap should be ok, as the input is utf8
* Refactor get_image_cloze_note
- fixes crash when note doesn't exist - Ok(None) case was not covered
- decouples business logic from native error->proto error conversion
- no need for original copy
- field[x] is more idiomatic than field.get(x).unwrap()
- don't need mutable access to fields
* Fix crash if image file unreadable
+ Use our read_file helper for better error context
* Add metadata() helper
* Fix crash if file metadata can't be read
* remove color picker, qmask and shape color
- remove strings from ftl
- remove color picker component
- remove from cloze generation
- remove icons for two buttons
- use constant color for shapes
* update color in reviewer and ftl strings
* fix shape position in canvas & add border to shape
- rename mask to inactive shape and active shape color
- border witdth and border color
- change decimal point deserializing string and toFixed(2)
- add thin border in mask editor, may be image background was transparent
* fix shape position in canvas after modified
- do not draw fixed ratio shapes by turn of uniformScaling
- fix rectangle width,height
- fix ellipse rx,ry,width,height
- fix polygon postion and points
- draw outside of canvas also
* fix border width and color in reviewer canvas
- rename variable
* refactor cloze div generate and remove angle
* fix origin when drawn outside of canvas from right
* fix shape at boundry & not include rx,ry rectangle
- move shapes at boundry when pointer is outside of canvas
- include rx, ry for ellipse only
- include points for polygon only
* fix lint errors & update image size in editor canvas based on height and width
* remove unsupported layerX & layerX for touchscreen
- fix shapes at edges
* implemented undo redo with canvas state
- implemented undo redo using fabric canvas events
- polygon is special case and implemented only added and modified event
- rectangle and ellipse have object:added, object:modified and object:removed case
- change id to undo and redo
* remove background image from canvas and used css to put image tag below canvas editor
- set image width and height after adding image
* fix for polygon points, add br in cloze strings, & toogle masks button
- fix shapes at edges
- toggle masks button to show/hide masks
- hide clozes string, it contains <br>
- set height for div container (used 'relative' in css)
* refactor top toolbar, add space and border radius
- rename cursor tools
- add left and right border
* fix undo after undo happen, use transparent color in draw mode
2023-03-29 04:33:19 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2023-04-26 08:01:04 +02:00
|
|
|
import * as tr from "@tslib/ftl";
|
|
|
|
|
Feature image occlusion (#2367)
* add note types with occlusions and image fields
* generate image occlusion cloze div data
- generate div element with data-* atrributes for canvas shape generate for reviewer
* getting image data & deck id and adding notes
the implementation added into backend
- added service index in backend.proto for image occlusion request
- created image_occlusion.proto with required message and service
- implementation in backend for getting image and adding notes, also during editing return imagecloze note and update notes
- add notes to selected deck, if no notetype then add image occlusion notetypes
- reuse notetype from stock notetypes when not exist
* script for generating shapes using canvas api in reviewer
- the flash issues fixed by loading image and using image size to draw canvas, also when image get resized, calculate scale using natural width and canvas width to draw shape at right position
- limit size of canvas for safari
* init image occlusion page in ts and build page
with
- fabricjs for editing shapes
- panzoom for drag and zoom
- pickr for color picker
- build page using web.rs
* implement top toolbar for canvas shapes
- undo & redo tools
- zoom in, zoom out and zoom fit
- group & ungroup
- copy & paste
- set transparency of shapes
- align tools
* implement side toolbar for drawing shapes
add top toolbar and the side toolbar contains following tools
- cursor for selecting shapes
- zoom for drag and zoom shapes in mask editor
- rectangle for creating it
- ellipse for creating it
- polygon for creating it using points
- shape fill color
- question mask color (currently only single color can be added for all shapes)
* add maskeditor page for editing mask
- add side toolbar and sidebar include toptoolbar
- load maskeditor in two mode
- for adding note using path to image
- for editing note using note id
* implement note editor page for adding notes
- the note editor page have simple button (B/I/U) and option to toggle html view
- option to select deck for adding notes into that deck
- option to generate to hide all, guess one & hide one, guess one notes
* add image occlusion page
add side toolbar, top toolbar, mask editor and note editor
- option to switch between mask editor and note editor
* implement generates notes and save notes
implemention to show toast components for messages
* removed pickr & implemented color picker component
- remove pickr
- implemented using html5 canvas
- range input for changing color
- another range input for opacity changes
- hex and rgba value support
* rename methods name & rust unwrap safety
- change plural names to singular
- create respone message in proto and return response with imagecloze note or error if not found with note id
- remove image_occlusion from post handler list
- rename service name in mediasrv.py
- rename methods name for image occlusion in backend and image_occlusion
- update frontend also for update functions' names
- handle error in frontend mask-editor.ts, when error getting notes then toast message shown to frontend
* extract to function & add comments & remove global
- extract function in mask-editor.ts to reduce duplicate
- remove unused global from css
- add comments to store.ts explaining usage
- changes id to noteId in lib.ts
- add comments for limitSize, becuase of duplicate implementation
* remove image_occlusion notetype
- remove from stock notetype, stdmodels
- add implementation for notetype to image occlusion
- add i18n for errors
* update smooth scroll, always show cursor tools
- change questionmask to qmask
- make selectable for shape true in all tools to simplify edits and draw shapes
- update image occlusion in reviewer ts to load image properly
* add and get notetype else return errors
* fix: not showing occlusion
* Use a oneof for ImageClozeNoteResponse
Makes it clearer that only one of them can be returned
* Don't crash if image filename not provided
The second unwrap should be ok, as the input is utf8
* Refactor get_image_cloze_note
- fixes crash when note doesn't exist - Ok(None) case was not covered
- decouples business logic from native error->proto error conversion
- no need for original copy
- field[x] is more idiomatic than field.get(x).unwrap()
- don't need mutable access to fields
* Fix crash if image file unreadable
+ Use our read_file helper for better error context
* Add metadata() helper
* Fix crash if file metadata can't be read
* remove color picker, qmask and shape color
- remove strings from ftl
- remove color picker component
- remove from cloze generation
- remove icons for two buttons
- use constant color for shapes
* update color in reviewer and ftl strings
* fix shape position in canvas & add border to shape
- rename mask to inactive shape and active shape color
- border witdth and border color
- change decimal point deserializing string and toFixed(2)
- add thin border in mask editor, may be image background was transparent
* fix shape position in canvas after modified
- do not draw fixed ratio shapes by turn of uniformScaling
- fix rectangle width,height
- fix ellipse rx,ry,width,height
- fix polygon postion and points
- draw outside of canvas also
* fix border width and color in reviewer canvas
- rename variable
* refactor cloze div generate and remove angle
* fix origin when drawn outside of canvas from right
* fix shape at boundry & not include rx,ry rectangle
- move shapes at boundry when pointer is outside of canvas
- include rx, ry for ellipse only
- include points for polygon only
* fix lint errors & update image size in editor canvas based on height and width
* remove unsupported layerX & layerX for touchscreen
- fix shapes at edges
* implemented undo redo with canvas state
- implemented undo redo using fabric canvas events
- polygon is special case and implemented only added and modified event
- rectangle and ellipse have object:added, object:modified and object:removed case
- change id to undo and redo
* remove background image from canvas and used css to put image tag below canvas editor
- set image width and height after adding image
* fix for polygon points, add br in cloze strings, & toogle masks button
- fix shapes at edges
- toggle masks button to show/hide masks
- hide clozes string, it contains <br>
- set height for div container (used 'relative' in css)
* refactor top toolbar, add space and border radius
- rename cursor tools
- add left and right border
* fix undo after undo happen, use transparent color in draw mode
2023-03-29 04:33:19 +02:00
|
|
|
window.addEventListener("load", () => {
|
|
|
|
window.addEventListener("resize", setupImageCloze);
|
|
|
|
});
|
|
|
|
|
|
|
|
export function setupImageCloze(): void {
|
2023-04-26 08:01:04 +02:00
|
|
|
window.requestAnimationFrame(setupImageClozeInner);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupImageClozeInner(): void {
|
Feature image occlusion (#2367)
* add note types with occlusions and image fields
* generate image occlusion cloze div data
- generate div element with data-* atrributes for canvas shape generate for reviewer
* getting image data & deck id and adding notes
the implementation added into backend
- added service index in backend.proto for image occlusion request
- created image_occlusion.proto with required message and service
- implementation in backend for getting image and adding notes, also during editing return imagecloze note and update notes
- add notes to selected deck, if no notetype then add image occlusion notetypes
- reuse notetype from stock notetypes when not exist
* script for generating shapes using canvas api in reviewer
- the flash issues fixed by loading image and using image size to draw canvas, also when image get resized, calculate scale using natural width and canvas width to draw shape at right position
- limit size of canvas for safari
* init image occlusion page in ts and build page
with
- fabricjs for editing shapes
- panzoom for drag and zoom
- pickr for color picker
- build page using web.rs
* implement top toolbar for canvas shapes
- undo & redo tools
- zoom in, zoom out and zoom fit
- group & ungroup
- copy & paste
- set transparency of shapes
- align tools
* implement side toolbar for drawing shapes
add top toolbar and the side toolbar contains following tools
- cursor for selecting shapes
- zoom for drag and zoom shapes in mask editor
- rectangle for creating it
- ellipse for creating it
- polygon for creating it using points
- shape fill color
- question mask color (currently only single color can be added for all shapes)
* add maskeditor page for editing mask
- add side toolbar and sidebar include toptoolbar
- load maskeditor in two mode
- for adding note using path to image
- for editing note using note id
* implement note editor page for adding notes
- the note editor page have simple button (B/I/U) and option to toggle html view
- option to select deck for adding notes into that deck
- option to generate to hide all, guess one & hide one, guess one notes
* add image occlusion page
add side toolbar, top toolbar, mask editor and note editor
- option to switch between mask editor and note editor
* implement generates notes and save notes
implemention to show toast components for messages
* removed pickr & implemented color picker component
- remove pickr
- implemented using html5 canvas
- range input for changing color
- another range input for opacity changes
- hex and rgba value support
* rename methods name & rust unwrap safety
- change plural names to singular
- create respone message in proto and return response with imagecloze note or error if not found with note id
- remove image_occlusion from post handler list
- rename service name in mediasrv.py
- rename methods name for image occlusion in backend and image_occlusion
- update frontend also for update functions' names
- handle error in frontend mask-editor.ts, when error getting notes then toast message shown to frontend
* extract to function & add comments & remove global
- extract function in mask-editor.ts to reduce duplicate
- remove unused global from css
- add comments to store.ts explaining usage
- changes id to noteId in lib.ts
- add comments for limitSize, becuase of duplicate implementation
* remove image_occlusion notetype
- remove from stock notetype, stdmodels
- add implementation for notetype to image occlusion
- add i18n for errors
* update smooth scroll, always show cursor tools
- change questionmask to qmask
- make selectable for shape true in all tools to simplify edits and draw shapes
- update image occlusion in reviewer ts to load image properly
* add and get notetype else return errors
* fix: not showing occlusion
* Use a oneof for ImageClozeNoteResponse
Makes it clearer that only one of them can be returned
* Don't crash if image filename not provided
The second unwrap should be ok, as the input is utf8
* Refactor get_image_cloze_note
- fixes crash when note doesn't exist - Ok(None) case was not covered
- decouples business logic from native error->proto error conversion
- no need for original copy
- field[x] is more idiomatic than field.get(x).unwrap()
- don't need mutable access to fields
* Fix crash if image file unreadable
+ Use our read_file helper for better error context
* Add metadata() helper
* Fix crash if file metadata can't be read
* remove color picker, qmask and shape color
- remove strings from ftl
- remove color picker component
- remove from cloze generation
- remove icons for two buttons
- use constant color for shapes
* update color in reviewer and ftl strings
* fix shape position in canvas & add border to shape
- rename mask to inactive shape and active shape color
- border witdth and border color
- change decimal point deserializing string and toFixed(2)
- add thin border in mask editor, may be image background was transparent
* fix shape position in canvas after modified
- do not draw fixed ratio shapes by turn of uniformScaling
- fix rectangle width,height
- fix ellipse rx,ry,width,height
- fix polygon postion and points
- draw outside of canvas also
* fix border width and color in reviewer canvas
- rename variable
* refactor cloze div generate and remove angle
* fix origin when drawn outside of canvas from right
* fix shape at boundry & not include rx,ry rectangle
- move shapes at boundry when pointer is outside of canvas
- include rx, ry for ellipse only
- include points for polygon only
* fix lint errors & update image size in editor canvas based on height and width
* remove unsupported layerX & layerX for touchscreen
- fix shapes at edges
* implemented undo redo with canvas state
- implemented undo redo using fabric canvas events
- polygon is special case and implemented only added and modified event
- rectangle and ellipse have object:added, object:modified and object:removed case
- change id to undo and redo
* remove background image from canvas and used css to put image tag below canvas editor
- set image width and height after adding image
* fix for polygon points, add br in cloze strings, & toogle masks button
- fix shapes at edges
- toggle masks button to show/hide masks
- hide clozes string, it contains <br>
- set height for div container (used 'relative' in css)
* refactor top toolbar, add space and border radius
- rename cursor tools
- add left and right border
* fix undo after undo happen, use transparent color in draw mode
2023-03-29 04:33:19 +02:00
|
|
|
const canvas: HTMLCanvasElement = document.querySelector("canvas")! as HTMLCanvasElement;
|
|
|
|
canvas.style.maxWidth = "100%";
|
|
|
|
canvas.style.maxHeight = "95vh";
|
|
|
|
|
|
|
|
const ctx: CanvasRenderingContext2D = canvas.getContext("2d")!;
|
2023-04-19 07:30:18 +02:00
|
|
|
const container = document.getElementById("container") as HTMLDivElement;
|
Feature image occlusion (#2367)
* add note types with occlusions and image fields
* generate image occlusion cloze div data
- generate div element with data-* atrributes for canvas shape generate for reviewer
* getting image data & deck id and adding notes
the implementation added into backend
- added service index in backend.proto for image occlusion request
- created image_occlusion.proto with required message and service
- implementation in backend for getting image and adding notes, also during editing return imagecloze note and update notes
- add notes to selected deck, if no notetype then add image occlusion notetypes
- reuse notetype from stock notetypes when not exist
* script for generating shapes using canvas api in reviewer
- the flash issues fixed by loading image and using image size to draw canvas, also when image get resized, calculate scale using natural width and canvas width to draw shape at right position
- limit size of canvas for safari
* init image occlusion page in ts and build page
with
- fabricjs for editing shapes
- panzoom for drag and zoom
- pickr for color picker
- build page using web.rs
* implement top toolbar for canvas shapes
- undo & redo tools
- zoom in, zoom out and zoom fit
- group & ungroup
- copy & paste
- set transparency of shapes
- align tools
* implement side toolbar for drawing shapes
add top toolbar and the side toolbar contains following tools
- cursor for selecting shapes
- zoom for drag and zoom shapes in mask editor
- rectangle for creating it
- ellipse for creating it
- polygon for creating it using points
- shape fill color
- question mask color (currently only single color can be added for all shapes)
* add maskeditor page for editing mask
- add side toolbar and sidebar include toptoolbar
- load maskeditor in two mode
- for adding note using path to image
- for editing note using note id
* implement note editor page for adding notes
- the note editor page have simple button (B/I/U) and option to toggle html view
- option to select deck for adding notes into that deck
- option to generate to hide all, guess one & hide one, guess one notes
* add image occlusion page
add side toolbar, top toolbar, mask editor and note editor
- option to switch between mask editor and note editor
* implement generates notes and save notes
implemention to show toast components for messages
* removed pickr & implemented color picker component
- remove pickr
- implemented using html5 canvas
- range input for changing color
- another range input for opacity changes
- hex and rgba value support
* rename methods name & rust unwrap safety
- change plural names to singular
- create respone message in proto and return response with imagecloze note or error if not found with note id
- remove image_occlusion from post handler list
- rename service name in mediasrv.py
- rename methods name for image occlusion in backend and image_occlusion
- update frontend also for update functions' names
- handle error in frontend mask-editor.ts, when error getting notes then toast message shown to frontend
* extract to function & add comments & remove global
- extract function in mask-editor.ts to reduce duplicate
- remove unused global from css
- add comments to store.ts explaining usage
- changes id to noteId in lib.ts
- add comments for limitSize, becuase of duplicate implementation
* remove image_occlusion notetype
- remove from stock notetype, stdmodels
- add implementation for notetype to image occlusion
- add i18n for errors
* update smooth scroll, always show cursor tools
- change questionmask to qmask
- make selectable for shape true in all tools to simplify edits and draw shapes
- update image occlusion in reviewer ts to load image properly
* add and get notetype else return errors
* fix: not showing occlusion
* Use a oneof for ImageClozeNoteResponse
Makes it clearer that only one of them can be returned
* Don't crash if image filename not provided
The second unwrap should be ok, as the input is utf8
* Refactor get_image_cloze_note
- fixes crash when note doesn't exist - Ok(None) case was not covered
- decouples business logic from native error->proto error conversion
- no need for original copy
- field[x] is more idiomatic than field.get(x).unwrap()
- don't need mutable access to fields
* Fix crash if image file unreadable
+ Use our read_file helper for better error context
* Add metadata() helper
* Fix crash if file metadata can't be read
* remove color picker, qmask and shape color
- remove strings from ftl
- remove color picker component
- remove from cloze generation
- remove icons for two buttons
- use constant color for shapes
* update color in reviewer and ftl strings
* fix shape position in canvas & add border to shape
- rename mask to inactive shape and active shape color
- border witdth and border color
- change decimal point deserializing string and toFixed(2)
- add thin border in mask editor, may be image background was transparent
* fix shape position in canvas after modified
- do not draw fixed ratio shapes by turn of uniformScaling
- fix rectangle width,height
- fix ellipse rx,ry,width,height
- fix polygon postion and points
- draw outside of canvas also
* fix border width and color in reviewer canvas
- rename variable
* refactor cloze div generate and remove angle
* fix origin when drawn outside of canvas from right
* fix shape at boundry & not include rx,ry rectangle
- move shapes at boundry when pointer is outside of canvas
- include rx, ry for ellipse only
- include points for polygon only
* fix lint errors & update image size in editor canvas based on height and width
* remove unsupported layerX & layerX for touchscreen
- fix shapes at edges
* implemented undo redo with canvas state
- implemented undo redo using fabric canvas events
- polygon is special case and implemented only added and modified event
- rectangle and ellipse have object:added, object:modified and object:removed case
- change id to undo and redo
* remove background image from canvas and used css to put image tag below canvas editor
- set image width and height after adding image
* fix for polygon points, add br in cloze strings, & toogle masks button
- fix shapes at edges
- toggle masks button to show/hide masks
- hide clozes string, it contains <br>
- set height for div container (used 'relative' in css)
* refactor top toolbar, add space and border radius
- rename cursor tools
- add left and right border
* fix undo after undo happen, use transparent color in draw mode
2023-03-29 04:33:19 +02:00
|
|
|
const image = document.getElementById("img") as HTMLImageElement;
|
2023-04-19 07:30:18 +02:00
|
|
|
if (image == null) {
|
2023-04-26 08:01:04 +02:00
|
|
|
container.innerText = tr.notetypeErrorNoImageToShow();
|
2023-04-19 07:30:18 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Feature image occlusion (#2367)
* add note types with occlusions and image fields
* generate image occlusion cloze div data
- generate div element with data-* atrributes for canvas shape generate for reviewer
* getting image data & deck id and adding notes
the implementation added into backend
- added service index in backend.proto for image occlusion request
- created image_occlusion.proto with required message and service
- implementation in backend for getting image and adding notes, also during editing return imagecloze note and update notes
- add notes to selected deck, if no notetype then add image occlusion notetypes
- reuse notetype from stock notetypes when not exist
* script for generating shapes using canvas api in reviewer
- the flash issues fixed by loading image and using image size to draw canvas, also when image get resized, calculate scale using natural width and canvas width to draw shape at right position
- limit size of canvas for safari
* init image occlusion page in ts and build page
with
- fabricjs for editing shapes
- panzoom for drag and zoom
- pickr for color picker
- build page using web.rs
* implement top toolbar for canvas shapes
- undo & redo tools
- zoom in, zoom out and zoom fit
- group & ungroup
- copy & paste
- set transparency of shapes
- align tools
* implement side toolbar for drawing shapes
add top toolbar and the side toolbar contains following tools
- cursor for selecting shapes
- zoom for drag and zoom shapes in mask editor
- rectangle for creating it
- ellipse for creating it
- polygon for creating it using points
- shape fill color
- question mask color (currently only single color can be added for all shapes)
* add maskeditor page for editing mask
- add side toolbar and sidebar include toptoolbar
- load maskeditor in two mode
- for adding note using path to image
- for editing note using note id
* implement note editor page for adding notes
- the note editor page have simple button (B/I/U) and option to toggle html view
- option to select deck for adding notes into that deck
- option to generate to hide all, guess one & hide one, guess one notes
* add image occlusion page
add side toolbar, top toolbar, mask editor and note editor
- option to switch between mask editor and note editor
* implement generates notes and save notes
implemention to show toast components for messages
* removed pickr & implemented color picker component
- remove pickr
- implemented using html5 canvas
- range input for changing color
- another range input for opacity changes
- hex and rgba value support
* rename methods name & rust unwrap safety
- change plural names to singular
- create respone message in proto and return response with imagecloze note or error if not found with note id
- remove image_occlusion from post handler list
- rename service name in mediasrv.py
- rename methods name for image occlusion in backend and image_occlusion
- update frontend also for update functions' names
- handle error in frontend mask-editor.ts, when error getting notes then toast message shown to frontend
* extract to function & add comments & remove global
- extract function in mask-editor.ts to reduce duplicate
- remove unused global from css
- add comments to store.ts explaining usage
- changes id to noteId in lib.ts
- add comments for limitSize, becuase of duplicate implementation
* remove image_occlusion notetype
- remove from stock notetype, stdmodels
- add implementation for notetype to image occlusion
- add i18n for errors
* update smooth scroll, always show cursor tools
- change questionmask to qmask
- make selectable for shape true in all tools to simplify edits and draw shapes
- update image occlusion in reviewer ts to load image properly
* add and get notetype else return errors
* fix: not showing occlusion
* Use a oneof for ImageClozeNoteResponse
Makes it clearer that only one of them can be returned
* Don't crash if image filename not provided
The second unwrap should be ok, as the input is utf8
* Refactor get_image_cloze_note
- fixes crash when note doesn't exist - Ok(None) case was not covered
- decouples business logic from native error->proto error conversion
- no need for original copy
- field[x] is more idiomatic than field.get(x).unwrap()
- don't need mutable access to fields
* Fix crash if image file unreadable
+ Use our read_file helper for better error context
* Add metadata() helper
* Fix crash if file metadata can't be read
* remove color picker, qmask and shape color
- remove strings from ftl
- remove color picker component
- remove from cloze generation
- remove icons for two buttons
- use constant color for shapes
* update color in reviewer and ftl strings
* fix shape position in canvas & add border to shape
- rename mask to inactive shape and active shape color
- border witdth and border color
- change decimal point deserializing string and toFixed(2)
- add thin border in mask editor, may be image background was transparent
* fix shape position in canvas after modified
- do not draw fixed ratio shapes by turn of uniformScaling
- fix rectangle width,height
- fix ellipse rx,ry,width,height
- fix polygon postion and points
- draw outside of canvas also
* fix border width and color in reviewer canvas
- rename variable
* refactor cloze div generate and remove angle
* fix origin when drawn outside of canvas from right
* fix shape at boundry & not include rx,ry rectangle
- move shapes at boundry when pointer is outside of canvas
- include rx, ry for ellipse only
- include points for polygon only
* fix lint errors & update image size in editor canvas based on height and width
* remove unsupported layerX & layerX for touchscreen
- fix shapes at edges
* implemented undo redo with canvas state
- implemented undo redo using fabric canvas events
- polygon is special case and implemented only added and modified event
- rectangle and ellipse have object:added, object:modified and object:removed case
- change id to undo and redo
* remove background image from canvas and used css to put image tag below canvas editor
- set image width and height after adding image
* fix for polygon points, add br in cloze strings, & toogle masks button
- fix shapes at edges
- toggle masks button to show/hide masks
- hide clozes string, it contains <br>
- set height for div container (used 'relative' in css)
* refactor top toolbar, add space and border radius
- rename cursor tools
- add left and right border
* fix undo after undo happen, use transparent color in draw mode
2023-03-29 04:33:19 +02:00
|
|
|
const size = limitSize({ width: image.naturalWidth, height: image.naturalHeight });
|
|
|
|
canvas.width = size.width;
|
|
|
|
canvas.height = size.height;
|
|
|
|
|
|
|
|
// set height for div container (used 'relative' in css)
|
|
|
|
container.style.height = `${image.height}px`;
|
|
|
|
|
|
|
|
// setup button for toggle image occlusion
|
|
|
|
const button = document.getElementById("toggle");
|
|
|
|
if (button) {
|
|
|
|
button.addEventListener("click", toggleMasks);
|
|
|
|
}
|
|
|
|
|
|
|
|
drawShapes(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawShapes(ctx: CanvasRenderingContext2D): void {
|
|
|
|
const activeCloze = document.querySelectorAll(".cloze");
|
|
|
|
const inActiveCloze = document.querySelectorAll(".cloze-inactive");
|
|
|
|
const shapeProperty = getShapeProperty();
|
|
|
|
|
|
|
|
for (const clz of activeCloze) {
|
|
|
|
const cloze = (<HTMLDivElement> clz);
|
|
|
|
const shape = cloze.dataset.shape!;
|
|
|
|
const fill = shapeProperty.activeShapeColor;
|
|
|
|
draw(ctx, cloze, shape, fill, shapeProperty.activeBorder);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const clz of inActiveCloze) {
|
|
|
|
const cloze = (<HTMLDivElement> clz);
|
|
|
|
const shape = cloze.dataset.shape!;
|
|
|
|
const fill = shapeProperty.inActiveShapeColor;
|
|
|
|
const hideinactive = cloze.dataset.hideinactive == "true";
|
|
|
|
if (!hideinactive) {
|
|
|
|
draw(ctx, cloze, shape, fill, shapeProperty.inActiveBorder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function draw(
|
|
|
|
ctx: CanvasRenderingContext2D,
|
|
|
|
cloze: HTMLDivElement,
|
|
|
|
shape: string,
|
|
|
|
color: string,
|
|
|
|
border: { width: number; color: string },
|
|
|
|
): void {
|
|
|
|
ctx.fillStyle = color;
|
|
|
|
|
|
|
|
const posLeft = parseFloat(cloze.dataset.left!);
|
|
|
|
const posTop = parseFloat(cloze.dataset.top!);
|
|
|
|
const width = parseFloat(cloze.dataset.width!);
|
|
|
|
const height = parseFloat(cloze.dataset.height!);
|
|
|
|
|
|
|
|
switch (shape) {
|
|
|
|
case "rect":
|
|
|
|
{
|
|
|
|
ctx.strokeStyle = border.color;
|
|
|
|
ctx.lineWidth = border.width;
|
|
|
|
ctx.fillRect(posLeft, posTop, width, height);
|
|
|
|
ctx.strokeRect(posLeft, posTop, width, height);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "ellipse":
|
|
|
|
{
|
|
|
|
const rx = parseFloat(cloze.dataset.rx!);
|
|
|
|
const ry = parseFloat(cloze.dataset.ry!);
|
|
|
|
const newLeft = posLeft + rx;
|
|
|
|
const newTop = posTop + ry;
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.strokeStyle = border.color;
|
|
|
|
ctx.lineWidth = border.width;
|
|
|
|
ctx.ellipse(newLeft, newTop, rx, ry, 0, 0, Math.PI * 2, false);
|
|
|
|
ctx.closePath();
|
|
|
|
ctx.fill();
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "polygon":
|
|
|
|
{
|
|
|
|
const points = JSON.parse(cloze.dataset.points!);
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.strokeStyle = border.color;
|
|
|
|
ctx.lineWidth = border.width;
|
|
|
|
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();
|
|
|
|
ctx.stroke();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// following function copy+pasted from mask-editor.ts,
|
|
|
|
// so update both, if it changes
|
|
|
|
function limitSize(size: { width: number; height: number }): { width: number; height: number; scalar: number } {
|
|
|
|
const maximumPixels = 1000000;
|
|
|
|
const { width, height } = size;
|
|
|
|
|
|
|
|
const requiredPixels = width * height;
|
|
|
|
if (requiredPixels <= maximumPixels) return { width, height, scalar: 1 };
|
|
|
|
|
|
|
|
const scalar = Math.sqrt(maximumPixels) / Math.sqrt(requiredPixels);
|
|
|
|
return {
|
|
|
|
width: Math.floor(width * scalar),
|
|
|
|
height: Math.floor(height * scalar),
|
|
|
|
scalar: scalar,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function getShapeProperty(): {
|
|
|
|
activeShapeColor: string;
|
|
|
|
inActiveShapeColor: string;
|
|
|
|
activeBorder: { width: number; color: string };
|
|
|
|
inActiveBorder: { width: number; color: string };
|
|
|
|
} {
|
|
|
|
const canvas = document.getElementById("canvas");
|
|
|
|
const computedStyle = window.getComputedStyle(canvas!);
|
|
|
|
// it may throw error if the css variable is not defined
|
|
|
|
try {
|
|
|
|
// shape color
|
|
|
|
const activeShapeColor = computedStyle.getPropertyValue("--active-shape-color");
|
|
|
|
const inActiveShapeColor = computedStyle.getPropertyValue("--inactive-shape-color");
|
|
|
|
// inactive shape border
|
|
|
|
const inActiveShapeBorder = computedStyle.getPropertyValue("--inactive-shape-border");
|
|
|
|
const inActiveBorder = inActiveShapeBorder.split(" ").filter((x) => x);
|
|
|
|
const inActiveShapeBorderWidth = parseFloat(inActiveBorder[0]);
|
|
|
|
const inActiveShapeBorderColor = inActiveBorder[1];
|
|
|
|
// active shape border
|
|
|
|
const activeShapeBorder = computedStyle.getPropertyValue("--active-shape-border");
|
|
|
|
const activeBorder = activeShapeBorder.split(" ").filter((x) => x);
|
|
|
|
const activeShapeBorderWidth = parseFloat(activeBorder[0]);
|
|
|
|
const activeShapeBorderColor = activeBorder[1];
|
|
|
|
|
|
|
|
return {
|
|
|
|
activeShapeColor: activeShapeColor ? activeShapeColor : "#ff8e8e",
|
|
|
|
inActiveShapeColor: inActiveShapeColor ? inActiveShapeColor : "#ffeba2",
|
|
|
|
activeBorder: {
|
|
|
|
width: activeShapeBorderWidth ? activeShapeBorderWidth : 1,
|
|
|
|
color: activeShapeBorderColor ? activeShapeBorderColor : "#212121",
|
|
|
|
},
|
|
|
|
inActiveBorder: {
|
|
|
|
width: inActiveShapeBorderWidth ? inActiveShapeBorderWidth : 1,
|
|
|
|
color: inActiveShapeBorderColor ? inActiveShapeBorderColor : "#212121",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch {
|
|
|
|
// return default values
|
|
|
|
return {
|
|
|
|
activeShapeColor: "#ff8e8e",
|
|
|
|
inActiveShapeColor: "#ffeba2",
|
|
|
|
activeBorder: {
|
|
|
|
width: 1,
|
|
|
|
color: "#212121",
|
|
|
|
},
|
|
|
|
inActiveBorder: {
|
|
|
|
width: 1,
|
|
|
|
color: "#212121",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const toggleMasks = (): void => {
|
|
|
|
const canvas = document.getElementById("canvas") as HTMLCanvasElement;
|
|
|
|
const display = canvas.style.display;
|
|
|
|
if (display === "none") {
|
|
|
|
canvas.style.display = "unset";
|
|
|
|
} else {
|
|
|
|
canvas.style.display = "none";
|
|
|
|
}
|
|
|
|
};
|