Skip to content

Commit

Permalink
Merge pull request #11 from will-moore/scale_thumbnails
Browse files Browse the repository at this point in the history
Scale thumbnails to fill space
  • Loading branch information
will-moore authored May 11, 2023
2 parents 54f299d + 7432cd1 commit 96347ee
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/Thumbnail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {
getDefaultColors,
} from "./util";

const MAX_LENGTH = 100;

export default function Thumbnail({ source, attrs }) {
// const [r, setChunk] = React.useState();

const [canvasSize, setCanvasSize] = React.useState({
width: 100,
height: 100,
width: MAX_LENGTH,
height: MAX_LENGTH,
});

const canvas = React.useRef();
Expand Down Expand Up @@ -75,10 +77,15 @@ export default function Thumbnail({ source, attrs }) {

let rbgData = renderTo8bitArray(ndChunks, minMaxValues, colors);

// setChunk(data);
let width = shape.at(-1);
let height = shape.at(-2);
setCanvasSize({ width, height });
const width = shape.at(-1);
const height = shape.at(-2);
let scale = width / MAX_LENGTH;
if (height > width) {
scale = height / MAX_LENGTH;
}
const cssWidth = width / scale;
const cssHeight = height / scale;
setCanvasSize({ width, height, cssWidth, cssHeight });

const ctx = canvas.current.getContext("2d");
ctx.putImageData(new ImageData(rbgData, width, height), 0, 0);
Expand All @@ -89,7 +96,7 @@ export default function Thumbnail({ source, attrs }) {

return (
<canvas
style={{ maxWidth: 100, maxHeight: 100, backgroundColor: 'lightgrey' }}
style={{ width: canvasSize.cssWidth, height: canvasSize.cssHeight, maxWidth: 100, maxHeight: 100, backgroundColor: 'lightgrey' }}
ref={canvas}
height={canvasSize.height}
width={canvasSize.width}
Expand Down

0 comments on commit 96347ee

Please sign in to comment.