Skip to content
This repository was archived by the owner on Aug 1, 2022. It is now read-only.

Commit eb34e41

Browse files
committed
Merge branch 'feature/exportToFile' of https://github.com/ern-arrowsmith/react-canvas-draw into develop
2 parents 80b1cec + 45826b1 commit eb34e41

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
"contributions": [
3030
"ideas"
3131
]
32+
},
33+
{
34+
"login": "ern-arrowsmith",
35+
"name": "Ernie Arrowsmith",
36+
"avatar_url": "https://avatars2.githubusercontent.com/u/3596444?s=400&u=c4cc9bf80c5572d07c8f877635b785b3ab99eee7&v=4",
37+
"profile": "https://www.linkedin.com/in/ernarrowsmith/",
38+
"contributions": [
39+
"ideas"
40+
]
3241
}
3342
]
3443
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ Useful functions that you can call, e.g. when having a reference to this compone
7676

7777
- `getSaveData()` returns the drawing's save-data as a stringified object
7878
- `loadSaveData(saveData: String, immediate: Boolean)` loads a previously saved drawing using the saveData string, as well as an optional boolean flag to load it immediately, instead of live-drawing it.
79+
- `getDataURL(fileType, useBgImage, backgroundColour)` will export the canvas to a data URL, which can subsequently be used to share or manipulate the image file.
7980
- `clear()` clears the canvas completely, including previously erased lines, and resets the view. After a clear, `undo()` will have no effect.
80-
- `eraseAll()` clears the drawn lines but retains their data; calling `undo()` can restore the erased lines. *Note: erased lines are not included in the save data.*
81+
- `eraseAll()` clears the drawn lines but retains their data; calling `undo()` can restore the erased lines. _Note: erased lines are not included in the save data._
8182
- `resetView()` resets the canvas' view to defaults. Has no effect if the `enablePanAndZoom` property is `false`.
8283
- `undo()` removes the latest change to the drawing. This includes everything drawn since the last MouseDown event.
8384

src/index.js

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,67 @@ export default class CanvasDraw extends PureComponent {
160160
});
161161
};
162162

163-
getDataURL = () => {
164-
//generates the image data url (JPG format) from the HTML5 canvas element
165-
return this.canvas.drawing.toDataURL();
163+
/**
164+
* Combination of work by Ernie Arrowsmith and emizz
165+
* References:
166+
* https://stackoverflow.com/questions/32160098/change-html-canvas-black-background-to-white-background-when-creating-jpg-image
167+
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
168+
169+
* This function will export the canvas to a data URL, which can subsequently be used to share or manipulate the image file.
170+
* @param {string} fileType Specifies the file format to export to. Note: should only be the file type, not the "image/" prefix.
171+
* For supported types see https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
172+
* @param {bool} useBgImage Specifies whether the canvas' current background image should also be exported. Default is false.
173+
* @param {string} backgroundColour The desired background colour hex code, e.g. "#ffffff" for white.
174+
*/
175+
getDataURL = (fileType, useBgImage, backgroundColour) => {
176+
// Get a reference to the "drawing" layer of the canvas
177+
let canvasToExport = this.canvas.drawing;
178+
179+
let context = canvasToExport.getContext("2d");
180+
181+
//cache height and width
182+
let width = canvasToExport.width;
183+
let height = canvasToExport.height;
184+
185+
//get the current ImageData for the canvas
186+
let storedImageData = context.getImageData(0, 0, width, height);
187+
188+
//store the current globalCompositeOperation
189+
var compositeOperation = context.globalCompositeOperation;
190+
191+
//set to draw behind current content
192+
context.globalCompositeOperation = "destination-over";
193+
194+
// If "useBgImage" has been set to true, this takes precedence over the background colour parameter
195+
if (useBgImage) {
196+
if (!this.props.imgSrc) return "Background image source not set";
197+
198+
// Write the background image
199+
this.drawImage();
200+
} else if (backgroundColour != null) {
201+
//set background color
202+
context.fillStyle = backgroundColour;
203+
204+
//fill entire canvas with background colour
205+
context.fillRect(0, 0, width, height);
206+
}
207+
208+
// If the file type has not been specified, default to PNG
209+
if (!fileType) fileType = "png";
210+
211+
// Export the canvas to data URL
212+
let imageData = canvasToExport.toDataURL(`image/${fileType}`);
213+
214+
//clear the canvas
215+
context.clearRect(0, 0, width, height);
216+
217+
//restore it with original / cached ImageData
218+
context.putImageData(storedImageData, 0, 0);
219+
220+
//reset the globalCompositeOperation to what it was
221+
context.globalCompositeOperation = compositeOperation;
222+
223+
return imageData;
166224
};
167225

168226
loadSaveData = (saveData, immediate = this.props.immediateLoading) => {

0 commit comments

Comments
 (0)