react-hybrid-canvas v1.0.0
react-hybrid-canvas
React component for scalable canvases backed by SVGs.
Oftentimes one would want to import an SVG into an HTML canvas, in order to do further raster processing with it. In React, this can be easily accomplished with refs and lifecycle methods that run after the DOM is updated. However, the drawback with such an approach is that since canvas operates upon bitmap, the key benefit of using SVG – the ability to scale according to device pixel density – is gone.
This React component solves the scaling problem, first by scaling the canvas
using devicePixelRatio when the component is mounted initially, and second
by listening for pixel ratio changes using matchMedia and resolution
CSS media query (with dppx units), in order to redraw the canvas
automatically when a change is detected (e.g. when the user zooms in, or when
the window is moved to another display).
Props
The following explicitly declared props apply to the underlying canvas. If you pass a prop that is not documented below, it will be passed along to the canvas anyway.
width,heightNumber: Base dimensions of the canvas. The actual canvas size will be scaled according todevicePixelRatio. Default:300×150.imageSmoothingEnabledBoolean: Whether or not image smoothing is to be enabled when rendering SVG image to the canvas. This is directly mapped toCanvasRenderingContext2D'simageSmoothingEnabledparameter. Default:true.imageSmoothingQualityString: Quality of image smoothing when rendering SVG image to the canvas. This is directly mapped toCanvasRenderingContext2D'simageSmoothingQualityparameter. Default:'high'.
These props control behaviors of this React component.
autoScaleBoolean: Listen for device pixel ratio changes and automatically rescale the canvas to accomodate. Default:true.drawDimensionsArray<Number>: Unscaled dimensions passed todrawImage, as an array of numbers. This component will automatically scale the actual dimensions used based on the current pixel ratio. The two-argument signature fordrawImageis not supported as it does not support scaling. Default:[0, 0, width, height], wherewidthandheightcorrespond to the props which fills the entire canvas.predrawFunction: This function is called every time a redraw is requested, before the SVG has already been drawn. It has the signature(ctx, scaleFactor)wherectxis aCanvasRenderingContext2Dassociated with the canvas, andscaleFactoris aNumberrepresenting the requested scale.drawFunction: This function is called every time a redraw is requested, after the SVG has already been drawn. It has the signature(ctx, scaleFactor)wherectxis aCanvasRenderingContext2Dassociated with the canvas, andscaleFactoris aNumberrepresenting the requested scale. Here is where you can do extra processing on the SVG canvas.onRatioChangeFunction: This function is called when the scale factor for the canvas changes due to changing device pixel ratio. It has the signature(scaleFactor), wherescaleFactoris equal to the current value ofdevicePixelRatio.
The SVG used in rendering can be supplied in two ways. If both are specified,
svgString takes precedence. If neither is specified, an empty SVG is used as
the default.
svgStringString: SVG to be rendered on the canvas as a string. This is the preferred method when the SVG is entirely static.svgElementReactElement: SVG to be rendered on the canvas as a React element.
Instance properties
If you get a ref of the HybridCanvas element, you will be able to access the
following properties and methods.
Properties
canvasHTMLCanvasElement: The underlying canvas DOM object.ctxCanvasRenderingContext2D: The 2D rendering context associated withcanvas.
Methods
toBlob(scaleFactor: Number) → Promise<Blob>: Convert the canvas to an image Blob, scaled at the providedscaleFactor. This uses thetoBlobmethod of anHTMLCanvasElement, but since the requestedscaleFactormay be different from the actual one of thecanvasproperty, a temporary canvas may be used.
9 years ago