@@ -5,7 +5,6 @@ let pointSize = isMobile ? .5 : 1;
55var lastX , lastY ;
66
77const drawCanvas = document . querySelector ( 'canvas#diagram-canvas' ) ;
8- const previewImage = document . querySelector ( '#preview-image' ) ;
98const imagePath = document . querySelector ( '#real-image' ) ;
109const ctx = drawCanvas . getContext ( '2d' ) ;
1110ctx . fillStyle = "transparent" ;
@@ -81,59 +80,39 @@ function toggleDrawCanvas() {
8180
8281function clear ( ) {
8382 ctx . clearRect ( 0 , 0 , drawCanvas . width , drawCanvas . height ) ;
84- drawCanvas . style . display = 'block' ;
85- previewImage . style . display = 'none' ;
8683}
8784
8885function downloadFile ( ) {
89- if ( drawCanvas . style . display === 'none' ) {
90- const a = document . createElement ( 'a' ) ;
91- a . style . display = 'none' ;
92- a . href = previewImage . src ;
93- a . download = 'diagram.png' ;
94- document . body . appendChild ( a ) ;
95- a . click ( ) ;
96- }
97- else {
9886 const a = document . createElement ( 'a' ) ;
9987 a . style . display = 'none' ;
10088 a . href = drawCanvas . toDataURL ( 'image/png' ) ;
10189 a . download = 'diagram.png' ;
10290 document . body . appendChild ( a ) ;
10391 a . click ( ) ;
104- }
10592}
10693
10794function addToPaper ( ) {
108-
109- if ( drawCanvas . style . display === 'none' )
110- {
111- const src = previewImage . src ;
112- document . querySelector ( '#note' ) . innerHTML = /* html */ `
113- <img style="width: 40%;" src="` + src + `" />
114- ` + document . querySelector ( '#note' ) . innerHTML ;
115- }
116- else {
117-
11895 document . querySelector ( '#note' ) . innerHTML = /* html */ `
11996 <img style="width: 100%;" src="${ drawCanvas . toDataURL ( 'image/png' ) } " />
12097 ` + document . querySelector ( '#note' ) . innerHTML ;
121- }
122-
12398 toggleDrawCanvas ( ) ;
12499}
125100
126101function addImageToPaper ( ) {
102+
103+ const tempImage = new Image ( ) ;
127104 imagePath . value = "" ;
128105 imagePath . click ( ) ;
129106 imagePath . addEventListener ( 'change' , function ( ) {
130107 const file = this . files [ 0 ] ;
131108 if ( file ) {
132109 const reader = new FileReader ( ) ;
133- previewImage . style . display = 'block' ;
134- drawCanvas . style . display = 'none' ;
135110 reader . addEventListener ( 'load' , function ( ) {
136- previewImage . setAttribute ( "src" , this . result ) ;
111+ tempImage . src = this . result ;
112+ tempImage . onload = function ( ) {
113+ ctx . drawImage ( tempImage , 0 , 0 , drawCanvas . width , drawCanvas . height ) ;
114+ }
115+ console . log ( drawCanvas ) ;
137116 } ) ;
138117 reader . readAsDataURL ( file )
139118 }
0 commit comments