Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 0.4.0

- Switch to Vite for bundling
- Remove `tsup`
- Update dependencies
- Drop support for React v15 & v16

## 0.3.0

- Use `tsup` for bundling
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,17 @@ import { BlurhashCanvas } from "react-blurhash";
## Browser support

Blurhash depends on `Uint8ClampedArray`, which is supported on all mainstream browsers and >=IE11.

## Development

### Build

```sh
npm run build
```

### Start Development Server

```sh
npm run dev
```
13 changes: 10 additions & 3 deletions demo/BlurhashImageEncoder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ const getImageData = (image: HTMLImageElement, resolutionX: number, resolutionY:
canvas.width = resolutionX;
canvas.height = resolutionY;
const context = canvas.getContext('2d');
if (!context) {
throw new Error('Failed to get 2D context');
}
context.drawImage(image, 0, 0, resolutionX, resolutionY);
return context.getImageData(0, 0, resolutionX, resolutionY);
};
Expand All @@ -127,7 +130,11 @@ const BlurhashImageEncoder: React.FunctionComponent<Props> = ({ onChange }) => {
[data, componentX, componentY],
);

useEffect(() => onChange(blurhash), [blurhash]);
useEffect(() => {
if (blurhash) {
onChange(blurhash);
}
}, [blurhash]);

const handleFileChange = useCallback((file: File) => {
const imageUrl = URL.createObjectURL(file);
Expand Down Expand Up @@ -162,7 +169,7 @@ const BlurhashImageEncoder: React.FunctionComponent<Props> = ({ onChange }) => {
min="1"
max="9"
value={componentX}
onChange={e => setComponentX(Number(e.target.value))}
onChange={(e) => setComponentX(Number(e.target.value))}
/>
</Setting>

Expand All @@ -171,7 +178,7 @@ const BlurhashImageEncoder: React.FunctionComponent<Props> = ({ onChange }) => {
min="1"
max="9"
value={componentY}
onChange={e => setComponentY(Number(e.target.value))}
onChange={(e) => setComponentY(Number(e.target.value))}
/>
</Setting>
</Settings>
Expand Down
21 changes: 10 additions & 11 deletions demo/Demo.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { useState, useMemo } from 'react';
import { hot } from 'react-hot-loader/root';
import styled from 'styled-components';
import { isBlurhashValid } from 'blurhash';

Expand Down Expand Up @@ -152,7 +151,7 @@ const BlurhashDemo = ({ hash }: { hash: string }) => {
max="1024"
step="8"
value={size}
onChange={e => setSize(Number(e.target.value))}
onChange={(e) => setSize(Number(e.target.value))}
/>
</Setting>

Expand All @@ -162,7 +161,7 @@ const BlurhashDemo = ({ hash }: { hash: string }) => {
max="128"
step="1"
value={resolution}
onChange={e => setResolution(Number(e.target.value))}
onChange={(e) => setResolution(Number(e.target.value))}
/>
</Setting>

Expand All @@ -172,7 +171,7 @@ const BlurhashDemo = ({ hash }: { hash: string }) => {
max="20"
step="1"
value={punch}
onChange={e => setPunch(Number(e.target.value))}
onChange={(e) => setPunch(Number(e.target.value))}
/>
</Setting>
</SettingsContainer>
Expand Down Expand Up @@ -204,7 +203,7 @@ const BlurhashCanvasDemo = ({ hash }: { hash: string }) => {
max="1024"
step="8"
value={size}
onChange={e => setSize(Number(e.target.value))}
onChange={(e) => setSize(Number(e.target.value))}
/>
</Setting>

Expand All @@ -214,7 +213,7 @@ const BlurhashCanvasDemo = ({ hash }: { hash: string }) => {
max="20"
step="1"
value={punch}
onChange={e => setPunch(Number(e.target.value))}
onChange={(e) => setPunch(Number(e.target.value))}
/>
</Setting>
</SettingsContainer>
Expand Down Expand Up @@ -242,29 +241,29 @@ const Demo = () => {
<StyledRadioInput
label="Blurhash string"
input={{
onChange: e => setMode(e.target.value as 'hash'),
onChange: (e) => setMode(e.target.value as 'hash'),
value: 'hash',
checked: mode === 'hash',
}}
/>
<StyledRadioInput
label="Encode image"
input={{
onChange: e => setMode(e.target.value as 'image'),
onChange: (e) => setMode(e.target.value as 'image'),
value: 'image',
checked: mode === 'image',
}}
/>
</ModeSelect>

{mode === 'hash' && (
<TextInput value={hashInput} onChange={e => setHashInput(e.target.value.trim())} />
<TextInput value={hashInput} onChange={(e) => setHashInput(e.target.value.trim())} />
)}

{mode === 'image' && (
<>
<Hint>Note: encoding is done in the browser only (no server involved)!</Hint>
<BlurhashImageEncoder onChange={hash => setEncodedHash(hash)} value={encodedHash} />
<BlurhashImageEncoder onChange={(hash) => setEncodedHash(hash)} value={encodedHash} />
</>
)}

Expand Down Expand Up @@ -312,4 +311,4 @@ const Demo = () => {
);
};

export default hot(Demo);
export default Demo;
4 changes: 2 additions & 2 deletions demo/FileInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ const FileInput: React.FunctionComponent<Props> = ({
const handleChange = useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => {
const { files } = event.currentTarget;
const newFile = files[0];
const newFile = files ? files[0] : undefined;

if (newFile !== file) {
if (newFile && newFile !== file) {
setFile(newFile);
onChange(newFile);
}
Expand Down
Loading