Welcome folks today in this blog post we will be exporting canvas to image file
using blob.js and save it as a png image
file using filesaver.js library. All the source code of the examples are given below.
Requirements
The requirements of the application is using two libraries
blob.js
filesaver.js
So you can include the libraries inside your project by a cdn
filesaver.js
cdn
1 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.0/FileSaver.min.js"></script> |
Now just make a blob.js
file inside your root folder and copy paste the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 |
/* Blob.js * A Blob, File, FileReader & URL implementation. * 2019-04-19 * * By Eli Grey, http://eligrey.com * By Jimmy Wärting, https://github.com/jimmywarting * License: MIT * See https://github.com/eligrey/Blob.js/blob/master/LICENSE.md */ ;(function () { var global = typeof window === 'object' ? window : typeof self === 'object' ? self : this var BlobBuilder = global.BlobBuilder || global.WebKitBlobBuilder || global.MSBlobBuilder || global.MozBlobBuilder global.URL = global.URL || global.webkitURL || function (href, a) { a = document.createElement('a') a.href = href return a } var origBlob = global.Blob var createObjectURL = URL.createObjectURL var revokeObjectURL = URL.revokeObjectURL var strTag = global.Symbol && global.Symbol.toStringTag var blobSupported = false var blobSupportsArrayBufferView = false var arrayBufferSupported = !!global.ArrayBuffer var blobBuilderSupported = BlobBuilder && BlobBuilder.prototype.append && BlobBuilder.prototype.getBlob try { // Check if Blob constructor is supported blobSupported = new Blob(['ä']).size === 2 // Check if Blob constructor supports ArrayBufferViews // Fails in Safari 6, so we need to map to ArrayBuffers there. blobSupportsArrayBufferView = new Blob([new Uint8Array([1, 2])]).size === 2 } catch (e) {} /** * Helper function that maps ArrayBufferViews to ArrayBuffers * Used by BlobBuilder constructor and old browsers that didn't * support it in the Blob constructor. */ function mapArrayBufferViews (ary) { return ary.map(function (chunk) { if (chunk.buffer instanceof ArrayBuffer) { var buf = chunk.buffer // if this is a subarray, make a copy so we only // include the subarray region from the underlying buffer if (chunk.byteLength !== buf.byteLength) { var copy = new Uint8Array(chunk.byteLength) copy.set(new Uint8Array(buf, chunk.byteOffset, chunk.byteLength)) buf = copy.buffer } return buf } return chunk }) } function BlobBuilderConstructor (ary, options) { options = options || {} var bb = new BlobBuilder() mapArrayBufferViews(ary).forEach(function (part) { bb.append(part) }) return options.type ? bb.getBlob(options.type) : bb.getBlob() } function BlobConstructor (ary, options) { return new origBlob(mapArrayBufferViews(ary), options || {}) } if (global.Blob) { BlobBuilderConstructor.prototype = Blob.prototype BlobConstructor.prototype = Blob.prototype } /********************************************************/ /* String Encoder fallback */ /********************************************************/ function stringEncode (string) { var pos = 0 var len = string.length var Arr = global.Uint8Array || Array // Use byte array when possible var at = 0 // output position var tlen = Math.max(32, len + (len >> 1) + 7) // 1.5x size var target = new Arr((tlen >> 3) << 3) // ... but at 8 byte offset while (pos < len) { var value = string.charCodeAt(pos++) if (value >= 0xd800 && value <= 0xdbff) { // high surrogate if (pos < len) { var extra = string.charCodeAt(pos) if ((extra & 0xfc00) === 0xdc00) { ++pos value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000 } } if (value >= 0xd800 && value <= 0xdbff) { continue // drop lone surrogate } } // expand the buffer if we couldn't write 4 bytes if (at + 4 > target.length) { tlen += 8 // minimum extra tlen *= (1.0 + (pos / string.length) * 2) // take 2x the remaining tlen = (tlen >> 3) << 3 // 8 byte offset var update = new Uint8Array(tlen) update.set(target) target = update } if ((value & 0xffffff80) === 0) { // 1-byte target[at++] = value // ASCII continue } else if ((value & 0xfffff800) === 0) { // 2-byte target[at++] = ((value >> 6) & 0x1f) | 0xc0 } else if ((value & 0xffff0000) === 0) { // 3-byte target[at++] = ((value >> 12) & 0x0f) | 0xe0 target[at++] = ((value >> 6) & 0x3f) | 0x80 } else if ((value & 0xffe00000) === 0) { // 4-byte target[at++] = ((value >> 18) & 0x07) | 0xf0 target[at++] = ((value >> 12) & 0x3f) | 0x80 target[at++] = ((value >> 6) & 0x3f) | 0x80 } else { // FIXME: do we care continue } target[at++] = (value & 0x3f) | 0x80 } return target.slice(0, at) } /********************************************************/ /* String Decoder fallback */ /********************************************************/ function stringDecode (buf) { var end = buf.length var res = [] var i = 0 while (i < end) { var firstByte = buf[i] var codePoint = null var bytesPerSequence = (firstByte > 0xEF) ? 4 : (firstByte > 0xDF) ? 3 : (firstByte > 0xBF) ? 2 : 1 if (i + bytesPerSequence <= end) { var secondByte, thirdByte, fourthByte, tempCodePoint switch (bytesPerSequence) { case 1: if (firstByte < 0x80) { codePoint = firstByte } break case 2: secondByte = buf[i + 1] if ((secondByte & 0xC0) === 0x80) { tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F) if (tempCodePoint > 0x7F) { codePoint = tempCodePoint } } break case 3: secondByte = buf[i + 1] thirdByte = buf[i + 2] if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) { tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F) if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) { codePoint = tempCodePoint } } break case 4: secondByte = buf[i + 1] thirdByte = buf[i + 2] fourthByte = buf[i + 3] if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) { tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F) if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) { codePoint = tempCodePoint } } } } if (codePoint === null) { // we did not generate a valid codePoint so insert a // replacement char (U+FFFD) and advance only 1 byte codePoint = 0xFFFD bytesPerSequence = 1 } else if (codePoint > 0xFFFF) { // encode to utf16 (surrogate pair dance) codePoint -= 0x10000 res.push(codePoint >>> 10 & 0x3FF | 0xD800) codePoint = 0xDC00 | codePoint & 0x3FF } res.push(codePoint) i += bytesPerSequence } var len = res.length var str = '' var i = 0 while (i < len) { str += String.fromCharCode.apply(String, res.slice(i, i += 0x1000)) } return str } // string -> buffer var textEncode = typeof TextEncoder === 'function' ? TextEncoder.prototype.encode.bind(new TextEncoder()) : stringEncode // buffer -> string var textDecode = typeof TextDecoder === 'function' ? TextDecoder.prototype.decode.bind(new TextDecoder()) : stringDecode function FakeBlobBuilder () { function isDataView (obj) { return obj && DataView.prototype.isPrototypeOf(obj) } function bufferClone (buf) { var view = new Array(buf.byteLength) var array = new Uint8Array(buf) var i = view.length while (i--) { view[i] = array[i] } return view } function array2base64 (input) { var byteToCharMap = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' var output = [] for (var i = 0; i < input.length; i += 3) { var byte1 = input[i] var haveByte2 = i + 1 < input.length var byte2 = haveByte2 ? input[i + 1] : 0 var haveByte3 = i + 2 < input.length var byte3 = haveByte3 ? input[i + 2] : 0 var outByte1 = byte1 >> 2 var outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4) var outByte3 = ((byte2 & 0x0F) << 2) | (byte3 >> 6) var outByte4 = byte3 & 0x3F if (!haveByte3) { outByte4 = 64 if (!haveByte2) { outByte3 = 64 } } output.push( byteToCharMap[outByte1], byteToCharMap[outByte2], byteToCharMap[outByte3], byteToCharMap[outByte4] ) } return output.join('') } var create = Object.create || function (a) { function c () {} c.prototype = a return new c() } if (arrayBufferSupported) { var viewClasses = [ '[object Int8Array]', '[object Uint8Array]', '[object Uint8ClampedArray]', '[object Int16Array]', '[object Uint16Array]', '[object Int32Array]', '[object Uint32Array]', '[object Float32Array]', '[object Float64Array]' ] var isArrayBufferView = ArrayBuffer.isView || function (obj) { return obj && viewClasses.indexOf(Object.prototype.toString.call(obj)) > -1 } } function concatTypedarrays (chunks) { var size = 0 var i = chunks.length while (i--) { size += chunks[i].length } var b = new Uint8Array(size) var offset = 0 for (i = 0, l = chunks.length; i < l; i++) { var chunk = chunks[i] b.set(chunk, offset) offset += chunk.byteLength || chunk.length } return b } /********************************************************/ /* Blob constructor */ /********************************************************/ function Blob (chunks, opts) { chunks = chunks || [] opts = opts == null ? {} : opts for (var i = 0, len = chunks.length; i < len; i++) { var chunk = chunks[i] if (chunk instanceof Blob) { chunks[i] = chunk._buffer } else if (typeof chunk === 'string') { chunks[i] = textEncode(chunk) } else if (arrayBufferSupported && (ArrayBuffer.prototype.isPrototypeOf(chunk) || isArrayBufferView(chunk))) { chunks[i] = bufferClone(chunk) } else if (arrayBufferSupported && isDataView(chunk)) { chunks[i] = bufferClone(chunk.buffer) } else { chunks[i] = textEncode(String(chunk)) } } this._buffer = global.Uint8Array ? concatTypedarrays(chunks) : [].concat.apply([], chunks) this.size = this._buffer.length this.type = opts.type || '' if (/[^\u0020-\u007E]/.test(this.type)) { this.type = '' } else { this.type = this.type.toLowerCase() } } Blob.prototype.arrayBuffer = function () { return Promise.resolve(this._buffer) } Blob.prototype.text = function () { return Promise.resolve(textDecode(this._buffer)) } Blob.prototype.slice = function (start, end, type) { var slice = this._buffer.slice(start || 0, end || this._buffer.length) return new Blob([slice], {type: type}) } Blob.prototype.toString = function () { return '[object Blob]' } /********************************************************/ /* File constructor */ /********************************************************/ function File (chunks, name, opts) { opts = opts || {} var a = Blob.call(this, chunks, opts) || this a.name = name.replace(/\//g, ':') a.lastModifiedDate = opts.lastModified ? new Date(opts.lastModified) : new Date() a.lastModified = +a.lastModifiedDate return a } File.prototype = create(Blob.prototype) File.prototype.constructor = File if (Object.setPrototypeOf) { Object.setPrototypeOf(File, Blob) } else { try { File.__proto__ = Blob } catch (e) {} } File.prototype.toString = function () { return '[object File]' } /********************************************************/ /* FileReader constructor */ /********************************************************/ function FileReader () { if (!(this instanceof FileReader)) { throw new TypeError("Failed to construct 'FileReader': Please use the 'new' operator, this DOM object constructor cannot be called as a function.") } var delegate = document.createDocumentFragment() this.addEventListener = delegate.addEventListener this.dispatchEvent = function (evt) { var local = this['on' + evt.type] if (typeof local === 'function') local(evt) delegate.dispatchEvent(evt) } this.removeEventListener = delegate.removeEventListener } function _read (fr, blob, kind) { if (!(blob instanceof Blob)) { throw new TypeError("Failed to execute '" + kind + "' on 'FileReader': parameter 1 is not of type 'Blob'.") } fr.result = '' setTimeout(function () { this.readyState = FileReader.LOADING fr.dispatchEvent(new Event('load')) fr.dispatchEvent(new Event('loadend')) }) } FileReader.EMPTY = 0 FileReader.LOADING = 1 FileReader.DONE = 2 FileReader.prototype.error = null FileReader.prototype.onabort = null FileReader.prototype.onerror = null FileReader.prototype.onload = null FileReader.prototype.onloadend = null FileReader.prototype.onloadstart = null FileReader.prototype.onprogress = null FileReader.prototype.readAsDataURL = function (blob) { _read(this, blob, 'readAsDataURL') this.result = 'data:' + blob.type + ';base64,' + array2base64(blob._buffer) } FileReader.prototype.readAsText = function (blob) { _read(this, blob, 'readAsText') this.result = textDecode(blob._buffer) } FileReader.prototype.readAsArrayBuffer = function (blob) { _read(this, blob, 'readAsText') // return ArrayBuffer when possible this.result = (blob._buffer.buffer || blob._buffer).slice() } FileReader.prototype.abort = function () {} /********************************************************/ /* URL */ /********************************************************/ URL.createObjectURL = function (blob) { return blob instanceof Blob ? 'data:' + blob.type + ';base64,' + array2base64(blob._buffer) : createObjectURL.call(URL, blob) } URL.revokeObjectURL = function (url) { revokeObjectURL && revokeObjectURL.call(URL, url) } /********************************************************/ /* XHR */ /********************************************************/ var _send = global.XMLHttpRequest && global.XMLHttpRequest.prototype.send if (_send) { XMLHttpRequest.prototype.send = function (data) { if (data instanceof Blob) { this.setRequestHeader('Content-Type', data.type) _send.call(this, textDecode(data._buffer)) } else { _send.call(this, data) } } } global.FileReader = FileReader global.File = File global.Blob = Blob } function fixFileAndXHR () { var isIE = !!global.ActiveXObject || ( '-ms-scroll-limit' in document.documentElement.style && '-ms-ime-align' in document.documentElement.style ) // Monkey patched // IE don't set Content-Type header on XHR whose body is a typed Blob // https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/6047383 var _send = global.XMLHttpRequest && global.XMLHttpRequest.prototype.send if (isIE && _send) { XMLHttpRequest.prototype.send = function (data) { if (data instanceof Blob) { this.setRequestHeader('Content-Type', data.type) _send.call(this, data) } else { _send.call(this, data) } } } try { new File([], '') } catch (e) { try { var klass = new Function('class File extends Blob {' + 'constructor(chunks, name, opts) {' + 'opts = opts || {};' + 'super(chunks, opts || {});' + 'this.name = name.replace(/\//g, ":");' + 'this.lastModifiedDate = opts.lastModified ? new Date(opts.lastModified) : new Date();' + 'this.lastModified = +this.lastModifiedDate;' + '}};' + 'return new File([], ""), File' )() global.File = klass } catch (e) { var klass = function (b, d, c) { var blob = new Blob(b, c) var t = c && void 0 !== c.lastModified ? new Date(c.lastModified) : new Date() blob.name = d.replace(/\//g, ':') blob.lastModifiedDate = t blob.lastModified = +t blob.toString = function () { return '[object File]' } if (strTag) { blob[strTag] = 'File' } return blob } global.File = klass } } } if (blobSupported) { fixFileAndXHR() global.Blob = blobSupportsArrayBufferView ? global.Blob : BlobConstructor } else if (blobBuilderSupported) { fixFileAndXHR() global.Blob = BlobBuilderConstructor } else { FakeBlobBuilder() } if (strTag) { File.prototype[strTag] = 'File' Blob.prototype[strTag] = 'Blob' FileReader.prototype[strTag] = 'FileReader' } var blob = global.Blob.prototype var stream function promisify(obj) { return new Promise(function(resolve, reject) { obj.onload = obj.onerror = function(evt) { obj.onload = obj.onerror = null evt.type === 'load' ? resolve(obj.result || obj) : reject(new Error('Failed to read the blob/file')) } }) } try { new ReadableStream({ type: 'bytes' }) stream = function stream() { var position = 0 var blob = this return new ReadableStream({ type: 'bytes', autoAllocateChunkSize: 524288, pull: function (controller) { var v = controller.byobRequest.view var chunk = blob.slice(position, position + v.byteLength) return chunk.arrayBuffer() .then(function (buffer) { var uint8array = new Uint8Array(buffer) var bytesRead = uint8array.byteLength position += bytesRead v.set(uint8array) controller.byobRequest.respond(bytesRead) if(position >= blob.size) controller.close() }) } }) } } catch (e) { try { new ReadableStream({}) stream = function stream(blob){ var position = 0 var blob = this return new ReadableStream({ pull: function (controller) { var chunk = blob.slice(position, position + 524288) return chunk.arrayBuffer().then(function (buffer) { position += buffer.byteLength var uint8array = new Uint8Array(buffer) controller.enqueue(uint8array) if (position == blob.size) controller.close() }) } }) } } catch (e) { try { new Response('').body.getReader().read() stream = function stream() { return (new Response(this)).body } } catch (e) { stream = function stream() { throw new Error('Include https://github.com/MattiasBuelens/web-streams-polyfill') } } } } if (!blob.arrayBuffer) { blob.arrayBuffer = function arrayBuffer() { var fr = new FileReader() fr.readAsArrayBuffer(this) return promisify(fr) } } if (!blob.text) { blob.text = function text() { var fr = new FileReader() fr.readAsText(this) return promisify(fr) } } if (!blob.stream) { blob.stream = stream } })() |
So now make a index.html
file and copy paste the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<!DOCTYPE html> <html> <head> <title>Exporting Canvas to Image in Javascript</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> </head> <body> <canvas id="canvas" width="980" height="320" style="border:1px solid #000;"> </canvas> </body> <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.0/FileSaver.min.js"></script> <script src="blob.js"></script> <script> var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.font = "30px Arial"; ctx.fillText("Hello World", 10, 50); canvas.toBlob(function(blob) { saveAs(blob, "prettyimage.png"); }); </script> </html> |
So you can see inside this block of code we are targeting the canvas
by their id and adding text to the canvas. Here we are using the font arial
and also we are writing hello world
at x coordinate 10 and y coordinate 50 respectively. And lastly we are using the toBlob
method to convert the canvas to blob object. And then we are converting the blob object to image file
. Here we are using the filesaver.js
library to actually convert the blob to image and download it as a png file
.
Adding Image in Canvas
So in this example we will be adding image
to canvas and then exporting to image
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<!DOCTYPE html> <html> <head> <title>Exporting Canvas to Image in Javascript</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> </head> <body> <img id="img" width="220" height="277" src="image.png" alt="The Scream"> <canvas id="canvas" width="980" height="320" style="border:1px solid #000;"> </canvas> </body> <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.0/FileSaver.min.js"></script> <script src="blob.js"></script> <script> var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.font = "30px Arial"; ctx.fillText("Hello World", 10, 50); var img = document.getElementById("img"); ctx.drawImage(img, 10, 10); canvas.toBlob(function(blob) { saveAs(blob, "prettyimage.png"); }); </script> </html> |
So in this block of code we are using drawImage
method to draw the image on the canvas.