Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from "react";
- import Barcode from "react-barcode";
- const saveImage = () => {
- const canvas = document.getElementById("canvas");
- var dataUrl = canvas.toDataURL();
- download(dataUrl);
- };
- const download = (source, filename = "image.png") => {
- const element = document.createElement("a");
- element.setAttribute("href", source);
- element.setAttribute("download", filename);
- element.style.display = "none";
- document.body.appendChild(element);
- element.click();
- document.body.removeChild(element);
- };
- const draw = () => {
- const canvas = document.getElementById("canvas");
- if (canvas.getContext) {
- const ctx = canvas.getContext("2d");
- ctx.fillRect(25, 25, 100, 100);
- ctx.clearRect(45, 45, 60, 60);
- ctx.strokeRect(50, 50, 50, 50);
- }
- };
- class App extends React.Component {
- componentDidMount() {
- draw();
- saveImage();
- }
- render() {
- return (<div>
- <canvas id="canvas" width="150" height="150"></canvas>
- </div>);
- }
- }
- export default App;
Add Comment
Please, Sign In to add comment