javierbyte / react-blur

React component to blur image backgrounds using canvas.
https://javier.xyz/react-blur/
BSD 3-Clause "New" or "Revised" License
460 stars 48 forks source link

Canvas to match Parent width / height #25

Open Howl0s opened 6 years ago

Howl0s commented 6 years ago

<div style={width: 900px}>
    <Blur style={{width: "100%"}} img={url}></Blur>
</div>

<div style={width: 500px}>
    <Blur style={{width: "100%"}} img={url}></Blur>
</div>

<div style={width: 300px}>
    <Blur style={{width: "100%"}} img={url}></Blur>
</div>

<div style={width: 100px}>
    <Blur style={{width: "100%"}} img={url}></Blur>
</div>
Error:
StackBlur.js:83 Uncaught Error: unable to access image data: IndexSizeError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The source height is 0.
Howl0s commented 6 years ago

fitToContainer(canvas);

function fitToContainer(canvas){
  // Make it visually fill the positioned parent
  canvas.style.width ='100%';
  canvas.style.height='100%';
  // ...then set the internal size to match
  canvas.width  = canvas.offsetWidth;
  canvas.height = canvas.offsetHeight;
}

A possible fix?