Skip to content

Commit

Permalink
Move dataURLtoBlob() to thymeUtility.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frodo161 committed Sep 17, 2023
1 parent 7c90f4c commit b60bbe0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
17 changes: 17 additions & 0 deletions app/assets/javascripts/thyme/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ const thymeUtility = {
thymeUtility.toHexaDecimal(red);
},

/*
Convert given dataURL to Blob, used for converting screenshot canvas to png.
*/
dataURLtoBlob: function(dataURL) {
// Decode the dataURL
const binary = atob(dataURL.split(',')[1]);
// Create 8-bit unsigned array
let array = [];
for (let i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
// Return our Blob object
return new Blob([new Uint8Array(array)], {
type: 'image/png'
});
},

/*
Lightens up a given color (given in a string in hexadecimal
representation "#xxyyzz") such that e.g. black becomes dark grey.
Expand Down
18 changes: 2 additions & 16 deletions app/assets/javascripts/thyme_editor.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
// convert given dataURL to Blob, used for converting screenshot canvas to png
function dataURLtoBlob(dataURL) {
// Decode the dataURL
const binary = atob(dataURL.split(',')[1]);
// Create 8-bit unsigned array
let array = [];
for (let i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
// Return our Blob object
return new Blob([new Uint8Array(array)], {
type: 'image/png'
});
};

$(document).on('turbolinks:load', function() {
/*
VIDEO INITIALIZATION
Expand All @@ -24,6 +9,7 @@ $(document).on('turbolinks:load', function() {
}
// initialize attributes
const video = document.getElementById('video-edit');
const mediumId = thymeEdit.dataset.medium;
thymeAttributes.video = video;
thymeAttributes.mediumId = thymeEdit.dataset.medium;

Expand Down Expand Up @@ -100,7 +86,7 @@ $(document).on('turbolinks:load', function() {
context.drawImage(video, 0, 0, canvas.width, canvas.height);
const base64image = canvas.toDataURL('image/png');
// Get our file
const file = dataURLtoBlob(base64image);
const file = thymeUtility.dataURLtoBlob(base64image);
// Create new form data
const fd = new FormData;
// Append our Canvas image file to the form data
Expand Down

0 comments on commit b60bbe0

Please sign in to comment.