Skip to content
Advertisement

Canvas linear gradient taking the value of another input type color field

So I have this simple image editor where I’m using a canvas to draw in a user-selected image and some texts. That is the user can upload an image and then if they want they can add texts or just change the gradient of the image.

Right now the app works perfectly fine except there is one issue.

How to find the issue? Do the following:

  1. Upload a random image.
  2. Choose a color from Text 2 Shadow Offset X Color Picker.
  3. Increase the slider beside the Text 2 Shadow Offset X Color Picker.
  4. Then increase the slider of Image Gradient and Opacity.

The image will have a gradient and take the value of the Text 2 Shadow Offset X Color Picker instead of Image Gradient and Opacity Color Picker which has default black color.

This is the code:

JavaScript
JavaScript
JavaScript

Rundown of the code:

1: First I have the fileUpload event listener. It takes in an image from the user and creates an image object and draws it on the canvas. Then imgManipulation function is called with the imgObj and event as its arguments.

  1. The imgManipulation function starts off with the input event listeners for the texts. That is whenever there is a change in the input, that is a user writes something, updateCanvas function is called.

  2. The updateCanvas function actually draws the texts on the image. I’m using a package called canvasTxt which helps the text to be multiline.

  3. The createGradient function call inside updateCanvas is the image gradient for the image.

I tried moving the createGradient function call below the drawTexts but then the gradient comes to the top of everything. The texts become darker too.

How can I make the gradient take it’s value only instead of shadow color?

Your help would be highly appreciated.

Thanks in advance

Advertisement

Answer

The shadowColor still work when drawing ‘Gradient’ next time. Add ctx.save() before, and ctx.restore() after.

JavaScript
JavaScript
JavaScript
Advertisement