transforming blog into a data URL

I've been struggling to convert an image blob into a data URL using Vue.js, but I keep encountering this error:

'Error in v-on handler: "TypeError: Failed to execute 'drawImage' on 'CanvasRenderingContext2D': The provided value is not of type '(CSSImageValue or HTMLCanvasElement or HTMLImageElement or HTMLVideoElement or ImageBitmap or OffscreenCanvas or SVGImageElement or VideoFrame)'."

My blob URL looks like this -

blob:http://localhost:8080/ca6d6419-f933-439b-8a16-62a80c81ab1a

Here's what I've attempted so far:

   upload() {
      this.value = blob:http://localhost:8080/ca6d6419-f933-439b-8a16-62a80c81ab1a;
      var canvas = document.createElement('canvas');
      canvas.width = this.value.width;
      canvas.height = this.value.height;
      canvas.getContext('2d').drawImage(this.value[0], 0, 0);
      dataURI = canvas.toDataURL();
      console.log(dataURI);
    },

Update:

   upload(){
      this.value = blob:http://localhost:8080/ca6d6419-f933-439b-8a16-62a80c81ab1a;
      let img = document.createElement('img');
      img.src = this.value;
      let canvas = document.createElement('canvas');
      canvas.width = img.width;
      canvas.height = img.height;
      canvas.getContext('2d').drawImage(img[0], 0, 0);
      dataURI = canvas.toDataURL();
      console.log(dataURI);
}

Answer №1

The issue arises with the drawImage function, as it requires one of the following as its first parameter: CSSImageValue, HTMLCanvasElement, HTMLImageElement, HTMLVideoElement, ImageBitmap, OffscreenCanvas, SVGImageElement, or VideoFrame.

To resolve this problem, you can create an image from your blob data:

var img = new Image();
img.src = URL.createObjectURL(yourBlob);

After loading the image, you can then use drawImage by passing the img as a parameter:

img.onload = function(){
   var canvas = document.createElement('canvas');
   canvas.width = this.value.width;
   canvas.height = this.value.height;
   canvas.getContext('2d').drawImage(img, 0, 0);
}

Answer №2

function transformBlobToUrl(blob) {
    var reader = new FileReader();
    reader.readAsDataURL(blob);
    reader.onloadend = function () {
        console.log(reader.result);
    };
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble utilizing NPM package within AWS Lambda environment, encountered issue with require() function

Recently, I developed a simple NPM package consisting of just two files. Here is the content of index.js: module.exports = { errors: { HttpError: require('./src/errors').HttpError, test: 'value' } } And here& ...

Personalize the file button for uploading images

I have a button to upload image files and I want to customize it to allow for uploading more than one image file. What is the logic to achieve this? <input type="file" />, which renders a choose button with text that says `no files chosen` in the sa ...

What is the best way to retrieve the value from a textfield in one module and use it in a

How can I access the value of a textField in another module within React.js without importing the entire textfield component? What is the most effective approach to get access to the value variable in a different module? Below is a sample functional textF ...

Is an audio player/playlist necessary for showcasing a mix engineer's skills in their

As a newcomer to the world of web development with some background knowledge from school, I work as a mix engineer and have created a portfolio website. Previously, I utilized Soundcloud and Spotify's API to showcase my mixes/songs, but the external J ...

Why is it that this particular line of code sometimes gives me an "undefined" result before returning an actual value?

Why does the method 'equal' always return with an "undefined" followed by a number? And when I try to parse it, it returns me a NaN. <!DOCTYPE> <html> <head> <script> var fnum = 0; var secondNum; var operation; functi ...

The information provided has been cut off: 1406 The content exceeds the permitted length for the 'image' column in row 1

Whenever I select a random image, the name turns out to be very long despite being a varchar data type. Here is my migration file: $table->id(); $table->string('image'); $table->timestamps(); This code snippet shows my image ...

The limitations of modifying a scope within an ng-if statement versus using a function call in AngularJS

Here are the code snippets demonstrating the toggling of the hideQA value. The value changes correctly in both conditions, but the divs are not hiding properly when using - `<button ng-click="hideQA = !hideQA">Reset</button>` However, if I ch ...

Exploring the Difference Between $onChanges and $onInit in Angular Components

What sets apart the use of Controller1 compared to Controller2? angular.module('app', []) .component('foo', { templateUrl: 'foo.html', bindings: { user: '<', }, controller: Controller1, ...

When using Vue with CSS3, placing an absolute positioned element within a relative wrapper can cause issues with maintaining the

Just starting out with Vue and diving into the world of CSS3! I'm currently working on a component that you can check out here: https://codesandbox.io/s/yjp674ppxj In essence, I have a ul element with relative positioning, followed by a series of di ...

Having trouble reaching the app.set variables?

Currently, I am developing a web app using expressjs. I am trying to figure out how to pass the port on which the app is running from the callingScript to the module: callingScript.js const http = require('http'); const app = require('./m ...

What methods does Angular use to handle bounded values?

Consider this straightforward Angular snippet: <input type="text" ng-model="name" /> <p>Hello {{name}}</p> When entering the text <script>document.write("Hello World!");</script>, it appears to be displayed as is without bei ...

Guide to making snackbars in vuetify version 3

I'd like to replicate the snackbars shown in this example using Vuetify 3. Can anyone guide me on how to achieve this? Check out the example here https://i.sstatic.net/RL53b.png App.vue <v-snackbar v-model="snackbar.visible" auto-h ...

Creating a variable by utilizing $index values within two nested v-for loops

For my project, I am attempting to organize the days of a month into a table using 2 v-for loops. To simplify my code, I am considering creating a t_index variable that would be equal to ((r_index - 1) * 7 + c_index) - 2, but I am unsure how to implement ...

What steps should I follow to successfully incorporate Zurb Foundation 4 Sections (tabs) into my Javascript Ajax functionality?

I am currently incorporating Zurb Foundation 4.1.5 into my application and I am faced with the challenge of implementing Zurb Section Javascript to handle "tabs" on the page. The content within these tabs is dynamic and fetched via Ajax calls. I am seeking ...

Unleashing the Power of Javascript in Selenium with Java (featuring PrimeFaces)

Recently, I discovered that JavaScript seems to be malfunctioning in my Selenium Tests written in Java. The reason behind this issue remains a mystery to me. Any ideas on how to tackle this roadblock? ((JavascriptExecutor) driver).executeScript("retur ...

Is it possible to have two unique keys in a single MongoDB schema?

Currently attempting to achieve this functionality using mongoose: const newContent = new Schema({ heading: { type: String, unique: true }, url: { type: String, unique: true }, //... }); However, an error is being thrown: MongoError: E11000 dupl ...

Adding Labels to Doughnut Charts in React using Chart.js 2.0

Currently, I'm exploring the world of data visualizations using react, react-chartjs-2, and chart.js version 2.2.1. While searching for a solution to my inquiry, I came across a potentially relevant answer on this link (specifically check out the upda ...

The FindProxyForURL function in a pac (proxy-auto-config) file is not compatible with the IE browser

After struggling for three days, we are still unable to solve a peculiar technical issue and now seek your assistance. The PAC (Proxy Auto-Config) file we have created works flawlessly in all browsers except for Internet Explorer (IE). The URL in questio ...

The process of building a Vue application encountered an error when attempting to run the command "npm run build"

I recently cloned a Vue application from GitHub (https://github.com/jimmerioles/progressive-weather-app) for setting up automated deployment in Jenkins. Before proceeding, I'm testing it on my Ubuntu machine (GCP VM). I have already installed Java, No ...

What is the best way to convert HTML into a React component?

This is the situation I am facing : 1) The application requests a CMS (Content Management System) for page contents. 2) The CMS responds with "<div>Hi,<SpecialButton color="red">My Button</SpecialButton></div>" 3) The applicat ...