canvas fillStyle in interval with transparent/opacity - javascript

This small code clear old canvas data in interval:
// start interval
ctx.save();
ctx.fillStyle = "rgba(0, 0, 0, 0.2)";
ctx.fillRect(0, 0, ctx.width, ctx.height);
ctx.restore();
//some draw code for new graph
...
//end interval
My work area become black, because I set black as fill color (rgba(0, 0, 0, .2)), but I need a transparent background, not black.
I tried use globalAlpha and imagePutData but I failed.
How I can do this?

I think this will resolve your issue
ctx.fillStyle = "rgba(255, 255, 255, 0.5)";

Using an rgba(0,0,0,.2) fillStyle and fillRect() works for me on both chrome and firefox - it paints a semi-transparent black fill. Check to make sure you're not doing something else that's causing a fully opaque paint of some sort.

try ctx.canvas.width, instead of ctx.width

Has your problem been solved
I encountered this problem while using Windows computer
I used opacity: .99 solved this problem

Related

I am trying to make an HSV color picker but I can't figure out how to generate the overlay image

So like I said in the title, I'm trying to make a color picker (with HTML/CSS/JS, if that matters) and I'm basing it off of DuckDuckGo's color picker (seen here).
They use a semi-transparent image overlayed on a div with a solid color. I thought that the image was just a vertical white --> black gradient and a horizontal and vertical opacity gradient. It turns out its more complicated than that.
I downloaded the image they use for their color picker and removed the alpha channel. I've uploaded it here. By the way, it appears they have subtle noise over it, for some reason.
I don't know what equation is used to generate this.
I do know that the alpha channel is equal to 1 - (x * y), but I don't know what algorithm is used to get the rgb channels.
I would also like to generate a similar image for an HSL color picker. I assume its the same algorithm, but scaled vertically by half and mirrored. Is that correct?
I solved it.
Draw a gradient going from solid white on the left to transparent white (rgba(255,255,255,0)) on the right, then drawing another gradient over top that using alpha blending which goes from solid black at the bottom to transparent black at the top.
Example code using the javascript canvas
var ctx = document.getElementByID("canvas").getContext("2d");
var saturationGradient = ctx.createLinearGradient(0, 0, 256, 0);
saturationGradient.addColorStop(0, "rgba(255, 255, 255, 1)"); //white
saturationGradient.addColorStop(1, "rgba(255, 255, 255, 0)"); //white transparent
var valueGradient = ctx.createLinearGradient(0, 0, 0, 256);
valueGradient.addColorStop(0, "rgba(0, 0, 0, 0)"); //transparent
valueGradient.addColorStop(1, "rgba(0, 0, 0, 1)"); //black
ctx.fillStyle = saturationGradient;
ctx.fillRect(0, 0, 256, 256);
ctx.fillStyle = valueGradient;
ctx.fillRect(0, 0, 256, 256);
The reason I couldn't figure it out was because I was using multiply blending instead of alpha blending.
I still haven't figured out a set of gradients for an HSL color picker.

JavaScript Canvas Transform Text

I have the following problem:
I need to transform text on a HTML canvas, e.g. give it a trapezoid shape.
Here's what I tried:
context.beginPath();
context.moveTo(0, 0);
context.lineTo(200, 0);
context.lineTo(100, -100);
context.lineTo(0, -100);
context.closePath();
context.clip();
context.fillText("Hello World", this.x, this.y);
As you might have guessed, the text gets cut off instead of transformed to fit the shape. Below are images of what I am trying to do and what I managed to do.
Any help is appreciated :)
What I managed to do:
What I want to do:
What you can do is get a dummy canvas element in which you put your normal text, then you can apply your own maths to the pixel color values in textCtx.getImageData onto another imageData array that you create, so you can then put that into another canvas with the correct transform with transformedTextCtx.putImageData, and finally draw its canvas onto the original context with ctx.drawImage.
I'm going to assume you know the kind of maths that you want to apply, and if not I suggest you look into skewing and scaling, and find out how you can combine the two.
The mdn resources for the imageData methods are in the "Pixel Manipulation" subsection of the canvas rendering context page: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D#Pixel_manipulation
General Answer: How to transform text
You can transform your text (or anything else) by using canvasContext.setTransform before drawing. You don't need any second canvas.
Examples:
canvasContext.setTransform(1, 0, 0, 1, 0, 0); is the default
canvasContext.setTransform(0.5, 0, 0, 1, 0, 0); reduces width to 50%
canvasContext.setTransform(1, 0, 0, 0.5, 0, 0); reduces height to 50%
Specific Answer:
I couldn't figure this out quickly. I'll leave it up to the next user to figure it out.

Slowly fade away image on canvas

I used the Draw Worm and made some changes and the result was this, but I have a problem to solve. I want to make the lines that have been there a long time to slowly fade away into darkness. I made this code:
function fadeOut() {
context.fillStyle = "rgba(0, 0, 0, 1)";
context.fillRect(0, 0, canvas.width, canvas.height);
setTimeout(fadeOut,10000);
}
fadeOut();
The problem is that the lines are disappearing too quickly, instead I wanted to obtain a gradual fade out, or more slowly.
That should be easy. The problem is that rgba(0, 0, 0, 1) means fully opaque black. You will probably want to try some semitransparent color. For example:
function fadeOut() {
context.fillStyle = "rgba(0, 0, 0, 0.05)";
context.fillRect(0, 0, canvas.width, canvas.height);
setTimeout(fadeOut,1000);
}
fadeOut();
I also recommend using requestAnimationFrame instead of setTimeout. I made a fiddle of your code, look how much nicer it is with fast animation: https://jsfiddle.net/Darker/mwj60hq4/

Workaround for Chrome arc path fill bug?

EDIT: This bug was fixed in version 38.
A recent version of Chrome introduced an issue in an application I maintain. I'm not sure if this is one of those weird "seems wrong but is actually correct" issues or if it's an honest-to-god bug, but it only presents in recent versions of Chrome (it started happening about a month ago, I'm not sure exactly which version introduced it)
The bug presents when using the context fill() method on certain paths that are drawn using the context arc() method. Rather than drawing a filled arc, what is filled is an oddly-shaped polygon.
Here's a demonstration of what I mean -- the shape in the upper right should be a filled arc:
var ctx = document.getElementById('cvs').getContext('2d');
// draw stroked arc
ctx.beginPath();
ctx.arc(75, 75, 50, 0, Math.PI/2);
ctx.lineTo(125, 125);
ctx.closePath();
ctx.stroke();
// draw filled arc
ctx.beginPath();
ctx.arc(225, 75, 50, 0, Math.PI/2);
ctx.lineTo(275, 125);
ctx.closePath();
ctx.fill();
// draw stroked triangle
ctx.beginPath();
ctx.moveTo(125, 225);
ctx.lineTo(75, 275);
ctx.lineTo(125, 275);
ctx.closePath();
ctx.stroke();
// draw filled triangle
ctx.beginPath();
ctx.moveTo(275, 225);
ctx.lineTo(225, 275);
ctx.lineTo(275, 275);
ctx.closePath();
ctx.fill();
<div><canvas id="cvs" width="300" height="300" style="border: solid black 1px"></canvas></div>
My question is this: is there a workaround for this issue? Preferably one that doesn't require me to write my own filled-arc renderer.
I do see this bug on Chrome 37.0.2062.124 on OS X. This may or may not be related to the bug described here, which is supposedly to be fixed in Chrome 38.
As a workaround, rotating a few degrees and immediately rotating it back before filling the arc seems to work.
// draw filled arc
ctx.beginPath();
ctx.arc(225, 75, 50, 0, Math.PI/2);
ctx.lineTo(275, 125);
ctx.closePath();
ctx.rotate(1*Math.PI/180); // Rotate 1 degree
ctx.rotate(-1*Math.PI/180); // Reverse rotation
ctx.fill();
Here's a fiddle demonstrating the workaround: http://jsfiddle.net/ejacpd1w/1/

Colourize an image in canvas?

I was wondering if there is a way to draw an image but give it a colourized effect.
If you consider games, when you want to place a sprite but theres an object in the way, often the object you try to place get a red tint to it to indicate it cannot be placed which is what im trying to achieve.
I currently draw it with an opacity here:
ctx.globalAlpha = 0.5
ctx.drawImage(image,abposx,abposy);
Is this possible to achieve without a library?
You can draw a semitransparent rectangle over top of it. For example:
ctx.fillStyle = 'rgba(255, 0, 0, 0.5)'; // 1/2 opacity red
ctx.fillRect(0, 0, 30, 30); // Draw this over top of your image
Here's a demo.
For isometric images, all you have to do is create the appropriate path. Here's an example of that:
You can clip the overlay to your image by setting the globalCompositeOperation to source-atop:
ctx.globalCompositeOperation = 'source-atop';
ctx.fillStyle = 'rgba(255, 0, 0, 0.5)'; // 1/2 opacity red
ctx.fillRect(0, 0, 30, 30); // Draw this over top of your image
ctx.globalCompositeOperation = 'source-over';
Here's a demo. You may also have to use a shadow canvas if the areas you're trying to draw on also have content, though.

Categories

Resources