Text not showing on canvas in HTML5 - javascript

I am trying to display text on the screen and it's not working for me.
Here is some of my code you give you an idea on what I made:
duck.js
class duck {
constructor (canvas, logs = false, start = () => {}, update = () => {}) {
document.documentElement.style.overflowX = 'hidden';
self.canvas = canvas;
self.ctx = self.canvas.getContext('2d');
self.logs = logs;
self.keys = {};
window.onkeyup = (e) => self.keys[e.keyCode] = false;
var dpi = window.devicePixelRatio;
var style_height = +getComputedStyle(self.canvas).getPropertyValue("height").slice(0, -2);
var style_width = +getComputedStyle(self.canvas).getPropertyValue("width").slice(0, -2);
self.canvas.setAttribute('height', style_height * dpi);
self.canvas.setAttribute('width', style_width * dpi);
self.init = () => {
var a;
self.logs ? console.info('Duck initialized!') : a = 0;
};
self.init.call();
start();
setInterval(update, 1);
};
rect (x = 0, y = 0, w = 1, h = 1, color = "#FFFFFF", fill = true) {
self.ctx.fillStyle = color;
fill ? self.ctx.fillRect(x, y, w, h): self.ctx.strokeRect(x, y, w, h);
}
fill (color = "#000000") {
self.ctx.fillStyle = color;
self.ctx.fillRect(0, 0, canvas.width, canvas.height);
}
text (x = 0, y = 0, text = 'Hello world!', align='left', font = '16px Verdana', color = '#000000') {
self.ctx.font = font;
console.log(self.ctx.font)
self.ctx.fillStyle = color;
console.log(self.ctx.fillStyle);
self.ctx.textAlign = align;
console.log(self.ctx.textAlign)
self.ctx.fillText(text, x, y);
}
get getWidth() {
return canvas.width;
}
get getHeight() {
return canvas.height;
}
screenshot() {
var image = self.canvas.toDataURL('image/png').replace("image/png", "image/octet-stream");
window.location.href = image;
}
keyDown(key = 'q') {
if (self.keys[key]) {return true} else {return false};
}
}
index.js
let duck_core = new duck(document.getElementById('main'), true, start, update);
function start() {
console.log('test');
}
function update() {
duck_core.fill('#FFFFFF');
duck_core.rect(0, 0, duck_core.getWidth, 10, '#222222');
duck_core.text(0, 0);
if (duck_core.keyDown('q')) {
duck_core.screenshot();
}
}

The reason why it wasn't showing is that the text has the center point on the bottom left corner and not the top right corner. To fix this, I added t the y position the size of my text, in this case, 16px.

Change self to this inside your duck class. In JavaScript, the global self variable is a reference to the window object, meaning it doesn't point to the instance of your class:
MDN
EDIT
You also have to update these parts:
get getWidth() {
return this.canvas.width; // added "this"
}
get getHeight() {
return this.canvas.height; // added "this"
}

Related

Moving multiple object together in canvas with respect to the current position

I have a canvas element with multiple images being displayed. I am trying to achieve is that when I click on a button it should move 200 position in X coordinates. I managed to move the second image, but the first image is not moving. And the images must move with respective to their current position. I am here by attaching my javascript for the same.
$(window).on('load', function () {
imageContent(200);
});
function imageContent(x) {
var posX = x;
var imgArray = ['http://via.placeholder.com/200x200?text=first', 'http://via.placeholder.com/200x200?text=second'];
$.each(imgArray, function (i, l) {
var img = new Image();
img.onload = function () {
var x = 0;
myCanvas(img, i * posX);
};
img.src = l;
});
}
function myCanvas(img, x) {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var last_ts = -1
var speed = 0.1
function renderScene() {
ctx.beginPath();
ctx.drawImage(img, x, 0);
}
function fly(ts) {
if (last_ts > 0) {
x += speed * (ts - last_ts)
}
last_ts = ts
if (x < 200) {
imageContent(x);
requestAnimationFrame(fly);
}
}
renderScene();
$('#movebutton').click(function () {
x = 0;
requestAnimationFrame(fly);
});
}
Here is the codepen. It would be greatful if anyone could help me.
Edit: Modified to achieve side by side movement
I modified your code a little, and removed jQuery. See if it is the effect you are trying to achieve:
var imgArr = [],
c = document.getElementById("myCanvas"),
ctx = c.getContext("2d"),
last_ts = -1,
speed = 0.1,
x = 0
window.onload = function() {
[
'http://via.placeholder.com/200x200?text=first',
'http://via.placeholder.com/200x200?text=second'
].forEach(function(obj, idx) {
var img = new Image()
img.onload = function() {
drawImage(img, idx * 200, 0)
}
img.src = obj
imgArr.push(img)
})
}
function drawImages(x) {
ctx.clearRect(0, 0, c.width, c.height)
imgArr.forEach(function(obj, idx) {
drawImage(obj, x + idx*200, 0)
})
}
function drawImage(img, x, y) {
ctx.beginPath()
ctx.drawImage(img, x, y)
}
function fly(ts) {
if (last_ts > 0) {
x += speed * (ts - last_ts)
}
last_ts = ts
if (x < 200) {
drawImages(x)
requestAnimationFrame(fly)
}
}
document
.getElementById('movebutton')
.addEventListener('click', function() {
x = 0
fly()
}, false)
canvas {
border: 1px solid red;
}
<canvas id="myCanvas" width="960" height="600"></canvas>
<button id="movebutton">Move</button>

Access object inside object without using function to get it?

I want to access an object but I don't know how.
I want to access 'ctx', but using base.ctx always returns null (is this a closure?).
window.base = function () {
var c = null,
ctx = null;
window.addEventListener("load", function(){
c = document.getElementById("canvas");
ctx = c.getContext("2d");
}
}
I found the solution was to call a function in base that returned ctx.
window.base = function () {
var c = null,
ctx = null;
window.addEventListener("load", function(){
c = document.getElementById("canvas");
ctx = c.getContext("2d");
}
return {
ctx: function(){return ctx;}
};
}
Then I can access ctx, but now all my accesses need to be:
base.ctx().setStyle = "white";
Rather than what I'd like which is:
base.ctx.setStyle = "white";
Is it possible? I'm thinking there's a possible solution with 'this'/scope or something, but I don't know enough JavaScript yet.
base is a function.
use it like this:
base().ctx()
I would write your code in this way:
window.addEventListener('load', function () {
var base = function () {
return document.getElementById("canvas").getContext("2d");
}
// Do something with base()
});
You can also drop the function and just use the context:
window.addEventListener('load', function () {
var base = document.getElementById("canvas").getContext("2d");
// Do something
});
The answer for me was to store the values in an object called "inter", and then update the contents of that object - the changes were then visible to other code.
So then I could use:
base.ctx.fillStyle = "white";
I have no idea why the other way didn't work!
var base = function () {
var fps = 60,
canvasWidth = 300,
canvasHeight = 200,
scaling = 1;
function updateCanvas(){
ctx.putImageData(imageData, 0, 0);
}
var c = null,
cStyle = null,
updateGraphicsCallBack = null;
function initialiseCanvas(canvasName, theCallBack){
updateGraphicsCallBack = theCallBack;
c = document.getElementById(canvasName);
cStyle = c.style;
inter.ctx = c.getContext("2d");
inter.imageData = inter.ctx.getImageData(0, 0, canvasWidth, canvasHeight);
inter.imageArray = inter.imageData.data;
inter.buff8 = new Uint8ClampedArray(inter.imageArray.buffer);
inter.buff32 = new Uint32Array(inter.imageArray.buffer);
cStyle.width = canvasWidth * scaling + "px";
cStyle.height = canvasHeight * scaling + "px";
c.width = canvasWidth;
c.height = canvasHeight;
inter.ctx.imageSmoothingEnabled = true;
inter.ctx.fillStyle="#909090";
//mouse = (typeof window.mouseInit !== "undefined") ? window.mouseInit(c) : null;
//if(typeof window.db !== "undefined") window.db.setOutput("debugOutput");
requestAnimationFrame(drawLoop);
}
var oldTimeStamp = 0, timeTweak = 0;
function drawLoop(currentTimeStamp) {
currentTime = currentTimeStamp;
if(typeof timeChart !== "undefined") timeChart.start();
setTimeout(
function() {
requestAnimationFrame(drawLoop);
}, (1000 - ((currentTimeStamp - oldTimeStamp) - timeTweak)) / fps);
updateGraphicsCallBack(currentTimeStamp);
if(typeof timeChart !== "undefined") {
timeChart.end();
if(timeChart.currentFrameCount() > -1){
var difference = timeChart.currentFrameCount() - fps;
timeTweak += difference;
if(timeTweak<-10000) timeTweak = -10000;
if(timeTweak>10000) timeTweak = 10000;
}
inter.timeTweak = timeTweak;
}
oldTimeStamp = currentTimeStamp;
}
var inter = {
updateCanvas: updateCanvas,
ctx: null,//function(){return ctx;},
canvasWidth: canvasWidth,
canvasHeight: canvasHeight,
fps: fps,
scaling: scaling,
imageData: null,
imageArray: null,
buff8: null,
buff32: null,
timeTweak: timeTweak,
initialiseCanvas: initialiseCanvas
};
return inter;
}();

render canvas circle in the centre of the browser window

I can't understand why my code isn't working,
the circle is being drawn but not in the centre of the canvas,
and it's much bigger than it should be and pix-elated.
Any suggestions, help greatly appreciated.
// safe code:
(function(global, library){
var spaceTime = function(container){
// if container is global object
if (typeof container === "object"){
var cHeight = container.innerHeight;
var cWidth = container.innerWidth;
}
// if container is a div box
else {
var cHeight = container.style.height;
var cWidth = container.style.width;
}
console.log("container is an "+typeof container);
// return unique instance each time sT is called
return new spaceTime.init(cHeight, cWidth);
}
// function constructor
spaceTime.init = function(height, width) {
this.height = height;
this.width = width;
}
// methods: override the default prototype property
spaceTime.prototype = {
checkDimensions: function() {
console.log(this.height + this.width);
console.log(this);
return this;
},
sequence: function(divide) {
// var getSize = this.getCentre(divide);
var grow = this.expand(200);
var box = this.setCanvas();
// box.classList.add("box");
document.body.appendChild(box);
// nest this in a requestAnimationFrame and pass
// the grow function to warp radius
this.drawCircle(box, grow);
return this;
},
// expands each unique instance.
expand: function(i) {
i += 1;
return i;
},
getCentre: function(axis) {
// width axis
if(axis === "x"){
console.log(this.height / 2);
return this.height / 2;
}
// height axis
else if(axis === "y"){
return this.width / 2;
}
// if undefined or the string doesn't match
else{
throw "please input an x or y axis to be proccessed";
}
},
// canvas context object and it's api methods
drawCircle: function(c, radius) {
var ctx = c.getContext("2d");
var x = this.getCentre("x");
var y = this.getCentre("y");
console.log(x);
console.log(y);
// define the arc path
ctx.beginPath();
ctx.arc(x, y, radius, 0, 2 * Math.PI, false);
ctx.fillStyle = 'green';
ctx.fill();
// stroke it
ctx.StrokeStyle = "#aaa";
ctx.stroke();
console.log(ctx);
console.log(radius);
},
// set up the canvas element
setCanvas: function() {
var c = document.createElement("canvas");
c.style.height = this.height + "px";
c.style.width = this.width + "px";
console.log("new canvas element created");
return c;
}
}
// override the prototype object with our own one.
spaceTime.init.prototype = spaceTime.prototype;
// reference and expose library to be invoked.
global.sT = spaceTime;
}(window));
// instance 1 of wormhole library
var inst1 = sT(this);
// method chaining
inst1.checkDimensions().sequence();
Didn't you confused x and y axises here?
getCentre: function(axis) {
if(axis === "x"){
return this.height / 2;
}
else if(axis === "y"){
return this.width / 2;
}
...
},
drawCircle: function(c, radius) {
...
var x = this.getCentre("x"); // will return this.height / 2
var y = this.getCentre("y"); // will return this.width / 2
console.log(x);
console.log(y);
...
}
Eh, probably, you shouldn't use style attribute when defining canvas size. Look here

How can I do a colour based hit test for a certain image on a web page?

I need to know when my mouse pointer is over a certain image, and while in that image, it goes over a very specific colour. Is this possible and how can I do it?
Just wrote this :
Basically, it does what #nepeo told you to do.
+ it cleans up the canvas and reset the original image after calculations.
+ I also added a customized cursor that you can disable.
Usage : Set your own callback function in colorPicker.callback, then attach the colorPicker.init function on the mouseover event of desired image.
N.B : You will have to host the images on your own server or to convert them to base64 in order to use this script.
var colorPicker = function() {};
//Settings
colorPicker.radius = 1, //square px
colorPicker.customCursor = true;
colorPicker.cursorColor = '#FFF';
//the fomatting for color return : "hex" || "rgba" || "array"
// !! hex is upperCase and returns "transparent" if opacity == 0; rgba is 'rgba(r,g,b,a)'; array is '[r,g,b,a]'
colorPicker.colorFormat = "hex";
//What to do with that color? Edit this to whatever you want
colorPicker.callback = function (color) {
var res = document.getElementById('ColorPickerResult');
res.style.backgroundColor = color;
res.style.color = invertHex(color);
res.innerHTML = color;
//Here I'm searching for some Hex color
if (color == '#FC771F') {
res.innerHTML = '!!! Found some orange !!!';
}
}
//What to do when we leave the image (Additionally to reset the original image)
colorPicker.callbackOut = function(){
var res = document.getElementById('ColorPickerResult');
res.innerHTML = '';
}
//END Settings
//This is optional
colorPicker.setCursor = function (canvas) {
if (!colorPicker.setCursor) return;
var can = document.createElement('canvas');
can.width = colorPicker.radius;
can.height = colorPicker.radius;
var ctx = can.getContext('2d');
ctx.strokeStyle = colorPicker.cursorColor;
ctx.rect(0, 0, colorPicker.radius, colorPicker.radius);
ctx.stroke();
canvas.style.cursor = 'url("' + can.toDataURL("image/png") + '"),default';
}
colorPicker.Init = function () {
var img = this;
var can = document.createElement('canvas');
can.id = 'ColorPicker';
can.width = img.width;
can.height = img.height;
can.ori = img;
can.oridisp = img.style.display;
var ctx = can.getContext('2d');
ctx.drawImage(img, 0, 0);
can.addEventListener('mousemove', colorPicker.pick);
can.addEventListener('mouseout', colorPicker.out);
colorPicker.setCursor(can);
img.parentNode.insertBefore(can, img);
img.style.display = 'none';
};
colorPicker.pick = function (evt) {
var rect = this.getBoundingClientRect();
ctx = this.getContext('2d'),
imageData = ctx.getImageData(evt.clientX - rect.left, evt.clientY - rect.top, colorPicker.radius, colorPicker.radius);
data = imageData.data;
r = 0, g = 0, b = 0, a = 0;
for (i = 0; i < data.length; i++) {
r += data[i];
g += data[++i];
b += data[++i];
a += data[++i];
}
var p = data.length / 4;
var mR = function (i) {
return Math.round(i);
};
r = mR(r/p);
g = mR(g/p);
b = mR(b/p);
a = (mR((a/p)/255 * 100)/100).toFixed(2);
if(colorPicker.colorFormat === "hex"){
var color = colorPicker.toHex(r,g,b,a);
}
else if(colorPicker.colorFormat === "rgba"){
var color = 'rgba('+r+', '+g+', '+b+', '+a+')';
}
else if(colorPicker.colorFormat === "array"){
var color = [r, g, b, a];
}
colorPicker.callback(color);
}
colorPicker.out = function () {
this.ori.style.display = this.oridisp;
this.parentNode.removeChild(this);
colorPicker.callbackOut();
}
//toHex function originally posted by http://stackoverflow.com/users/10474/felipec in http://stackoverflow.com/a/19765382/3702797
colorPicker.toHex= function(r,g,b,a){
if(a=="0.00") return "transparent";
var rgb = b | (g << 8) | (r << 16);
return '#' + (0x1000000 + rgb).toString(16).slice(1).toUpperCase()
}
// END colorPicker \\
// Just an example of external function beeing called
function invertHex(c){
if(c==="transparent") return "#000000";
c = c.replace('#', '');
var reqHex = "#";
for(var i=0;i<6;i++){
reqHex = reqHex + (15-parseInt(c[i],16)).toString(16);
}
return reqHex;
}
//Attach the function to whatever images you want
document.getElementsByTagName('img')[0].addEventListener('mouseover', colorPicker.Init);
body {
background: #175;
}
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAO4AAABDCAYAAACbbDkSAAAKQWlDQ1BJQ0MgUHJvZmlsZQAASA2dlndUU9kWh8+9N73QEiIgJfQaegkg0jtIFQRRiUmAUAKGhCZ2RAVGFBEpVmRUwAFHhyJjRRQLg4Ji1wnyEFDGwVFEReXdjGsJ7601896a/cdZ39nnt9fZZ+9917oAUPyCBMJ0WAGANKFYFO7rwVwSE8vE9wIYEAEOWAHA4WZmBEf4RALU/L09mZmoSMaz9u4ugGS72yy/UCZz1v9/kSI3QyQGAApF1TY8fiYX5QKUU7PFGTL/BMr0lSkyhjEyFqEJoqwi48SvbPan5iu7yZiXJ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dKH9euRhfR3HSEi6fTB6eJD+eojPf6sunuIAfYepu+Hl5e2UwTI3nQN0sOdxjPHxk3p6B1gdMf2hb1y4D9TM6N/WJ67GojaIC3bTLnuflY74tnIKRixadXKqtjH+5FwQKAgWBgkBBoCBQECgIFAQKAgWBgkBBoCBQECgIFAQKAgWBgkBBoCBQECgIFAQKAgWBgkBBoCBQECgIFAQKAgWBdxOB/wcP4cIU+D70agAAAABJRU5ErkJggg==" />
<span id="ColorPickerResult"></span>
Here I made an attempt. In this example the script searches for the color #FF0000, as passed in the options object. http://jsfiddle.net/2sw3pzs4/
var ColorChecker = function( options ) {
var canvas, ctx, output;
this.init = function() {
canvas = document.getElementById('canvas');
output = document.getElementById('output');
ctx = canvas.getContext("2d");
color = this.hexToRGB(options.searchedColor);
this.drawOnCanvas();
canvas.addEventListener('mousemove', function(e) {
if(e.offsetX) {
mouseX = e.offsetX;
mouseY = e.offsetY;
}
else if(e.layerX) {
mouseX = e.layerX;
mouseY = e.layerY;
}
var c = ctx.getImageData(mouseX, mouseY, 1, 1).data;
var foundColor = [c[0], c[1], c [2]];
var is_identical = foundColor.join() === color.join();
var message = is_identical == true ? 'red found' : 'not found';
output.innerHTML = message;
});
}
this.drawOnCanvas = function() {
ctx.fillStyle = "#FF0000";
ctx.fillRect(0,0, 20, 20);
}
this.hexToRGB = function( hex ) {
var r = parseInt((this.cutHex(hex)).substring(0,2),16);
var g = parseInt((this.cutHex(hex)).substring(2,4),16);
var b = parseInt((this.cutHex(hex)).substring(4,6),16);
return [r,g,b];
}
this.cutHex = function( hex ) {
return hex.replace("#",'');
}
this.init();
}
var colorChecker = new ColorChecker({
searchedColor: "#FF0000",
});
If you want to use an image just use putImageData() to draw the image on the canvas. You can use the function drawOnCanvas in my code to draw whatever you want inside the canvas, or resize it or other stuff.
Use a canvas instead of the image, resize the canvas to the size of the image and draw the image into the canvas.
Then you can get the pixel location of the cursor using the canvas.onmousemove event. Canvas may have something in it's api for querying the colour at a location(can't remember at this moment) but otherwise you can use getImageData to get the rgba data for every pixel and do a look up during the event. Ta da!

How to store Brush Size and Brush Color in a Drawing Web app

So I want to create a drawing app, and I did, I am able to change brush size and brush color, however, I want to save the stuff I draw including the brush size and the brush color. I am able to store the point I have drawn on the canvas now, but not the brush size and color.
var canvas;
var context;
var color = "black";
var brushSize = 13;
var mouseDown = false;
window.onload = function init() {
//Brush Size
var bigBrushSize = document.getElementById("bigBrush");
bigBrushSize.onclick = handleBigBrushclicked;
var mediumBrushSize = document.getElementById("mediumBrush");
mediumBrushSize.onclick = handleMediumBrushclicked;
var smallBrushSize = document.getElementById("smallBrush");
smallBrushSize.onclick = handleSmallBrushclicked;
//Brush Color
var redBrushColor = document.getElementById("red");
redBrushColor.onclick = handleRedBrushColorclicked;
var blueBrushColor = document.getElementById("blue");
blueBrushColor.onclick = handleBlueBrushColorclicked;
var yellowBrushColor = document.getElementById("yellow");
yellowBrushColor.onclick = handleYellowBrushColorclicked;
var greenBrushColor = document.getElementById("green");
greenBrushColor.onclick = handleGreenBrushColorclicked;
//Clear Button
var clearButton = document.getElementById("clearButton");
clearButton.onclick = handleClearButton;
canvas = document.getElementById("myCanvas");
context = canvas.getContext("2d");
canvas.onmousedown = handleMouseDown;
canvas.onmouseup = handleMouseUp;
canvas.onmousemove = handleMouseMove;
var coords = localStorage["coords"];
if (coords) {
coords = JSON.parse(coords);
for (var i = 0; i < coords.length; i = i + 2) {
paintToCanvas(coords[i], coords[i + 1]);
}
}
}
function handleClearButton() {
context.clearRect(0, 0, canvas.width, canvas.height);
localStorage.removeItem("coords");
}
function handleMouseDown(event) {
paintFromMouse(event);
mouseDown = true;
}
function handleMouseUp(event) {
mouseDown = false;
}
function handleMouseMove(event) {
if (mouseDown) {
paintFromMouse(event);
}
}
// Bursh Size Start
function handleBigBrushclicked() {
brushSize = 32;
}
function handleMediumBrushclicked() {
brushSize = 16;
}
function handleSmallBrushclicked() {
brushSize = 6;
}
// Brush Size Ends
function paintFromMouse(event) {
var x = event.clientX - canvas.offsetLeft;
var y = event.clientY - canvas.offsetTop;
paintToCanvas(x, y);
var coords = localStorage["coords"];
if (coords) {
coords = JSON.parse(coords);
} else {
coords = [];
}
coords.push(x);
coords.push(y);
localStorage.setItem("coords", JSON.stringify(coords));
}
//color change starts
function handleRedBrushColorclicked() {
color = "red";
}
function handleBlueBrushColorclicked() {
color = "blue";
}
function handleGreenBrushColorclicked() {
color = "green";
}
function handleYellowBrushColorclicked() {
color = "yellow";
}
// color change ends
function paintToCanvas(x, y) {
context.fillStyle = color;
context.fillRect(x - brushSize / 2, y - brushSize / 2, brushSize, brushSize);
So here I used the local storage to store brush size and color, the alert shows it seems to work, but I still don't know how to actually store them, which means, if I refresh my page, nothing changes on my canvas.
var storeColorAndBrush = [color " ", brushSize]
var store = storeColorAndBrush
localStorage.setItem("store",store)
alert(localStorage.store);
}
Thank you very much, I am a beginner.
This is how I would do it: I would add a "save" button which, when clicked, stores the whole image and the current brush size and color. When you load the page you draw the stored image in the canvas and set the brush size and color:
$('.save').on('click',function() {
var data = context.getImageData(x, y, img.width, img.height).data;
localStorage.setItem('image', data); // save image data
localStorage.setItem('brushSize', brushSize);
localStorage.setItem('color', color);
});
on load:
brushSize = localStorage.getItem('brushSize');
color = localStorage.getItem('color');
var picture = localStorage.getItem('image');
context.putImageData(picture, 0, 0);

Categories

Resources