Skip to content

Commit

Permalink
fix: 🐛 resize method to validate and update the width,height (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf authored Jan 26, 2024
1 parent d1bd25d commit ad2000e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 26 deletions.
12 changes: 12 additions & 0 deletions dotlottie-rs/src/lottie_renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub enum LottieRendererError {

#[error("Invalid color: {0}")]
InvalidColor(String),

#[error("Invalid argument: {0}")]
InvalidArgument(String),
}

pub struct LottieRenderer {
Expand Down Expand Up @@ -244,6 +247,15 @@ impl LottieRenderer {
return Ok(());
}

if width <= 0 || height <= 0 {
return Err(LottieRendererError::InvalidArgument(
"Width and height must be greater than 0".to_string(),
));
}

self.width = width;
self.height = height;

let mut buffer = vec![0; (width * height * 4) as usize];
thorvg_canvas
.set_target(
Expand Down
73 changes: 47 additions & 26 deletions web-example.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<canvas style="width: 100%; border: 1px solid black"></canvas>
<button id="play">play</button>
<button id="pause">pause</button>
<button id="resize">resize</button>
<input
id="progress-bar"
type="range"
Expand All @@ -34,6 +35,7 @@

const playBtn = document.querySelector("#play");
const pauseBtn = document.querySelector("#pause");
const resizeBtn = document.querySelector("#resize");
const progressBar = document.querySelector("#progress-bar");
const jumpBtn = document.querySelector("#jump");
const modeSelect = document.querySelector("#mode");
Expand Down Expand Up @@ -82,17 +84,14 @@

const canvas = document.querySelector("canvas");

const { width: clientWidth, height: clientHeight } =
canvas.getBoundingClientRect();

const width = clientWidth * (window.devicePixelRatio || 1);
const height = clientHeight * (window.devicePixelRatio || 1);

canvas.width = width;
canvas.height = height;

// const loaded = dotLottiePlayer.loadAnimationData(data, width, height);
const loaded = dotLottiePlayer.loadDotLottieData(data, width, height);
const loaded = dotLottiePlayer.loadDotLottieData(
data,
canvas.width,
canvas.height
);

resize();

dotLottiePlayer.setConfig({
...dotLottiePlayer.config(),
Expand All @@ -103,7 +102,6 @@
console.log("Loaded: ", loaded);

const ctx = canvas.getContext("2d");
const imageData = ctx.createImageData(width, height);

let animationFrameId = null;

Expand All @@ -117,13 +115,7 @@
(dotLottiePlayer.currentFrame() / dotLottiePlayer.totalFrames()) *
100;

const rendered = dotLottiePlayer.render();

if (rendered) {
const frameBuffer = dotLottiePlayer.buffer();
imageData.data.set(frameBuffer);
ctx.putImageData(imageData, 0, 0);
}
render();

if (dotLottiePlayer.isComplete()) {
if (dotLottiePlayer.config().loopAnimation) {
Expand Down Expand Up @@ -151,6 +143,10 @@
dotLottiePlayer.setFrame(44);
});

resizeBtn.addEventListener("click", () => {
resize();
});

progressBar.addEventListener("mousedown", () => {
cancelAnimationFrame(animationFrameId);
});
Expand All @@ -163,17 +159,34 @@
const newFrame =
(event.target.value / 100) * dotLottiePlayer.totalFrames();

const updated = dotLottiePlayer.setFrame(newFrame);
if (updated) {
const rendered = dotLottiePlayer.render();
if (rendered) {
const frameBuffer = dotLottiePlayer.buffer();
imageData.data.set(frameBuffer);
ctx.putImageData(imageData, 0, 0);
}
if (dotLottiePlayer.setFrame(newFrame)) {
render();
}
});

function resize() {
const { width: clientWidth, height: clientHeight } =
canvas.getBoundingClientRect();

const width = clientWidth * (window.devicePixelRatio || 1);
const height = clientHeight * (window.devicePixelRatio || 1);

canvas.width = width;
canvas.height = height;

dotLottiePlayer.resize(width, height);
}

function render() {
const rendered = dotLottiePlayer.render();
if (rendered) {
const frameBuffer = dotLottiePlayer.buffer();
const imageData = ctx.createImageData(canvas.width, canvas.height);
imageData.data.set(frameBuffer);
ctx.putImageData(imageData, 0, 0);
}
}

modeSelect.addEventListener("change", (event) => {
const mode = Module.Mode.values[event.target.value];
dotLottiePlayer.setConfig({
Expand Down Expand Up @@ -210,3 +223,11 @@
</script>
</body>
</html>

<!--
buffer_ptr: 0xa03f98
buffer_len: 2024064
buffer_ptr: 0x11bc9a0
buffer_len: 1460864
-->

0 comments on commit ad2000e

Please sign in to comment.