So I'm trying to create a Meme generator using the HTML5 canvas. I understand I can write on the canvas using the following:
ctx.filltext()
However, I can only write on the canvas once. Does anyone know how I could write on the canvas more than once or even twice as in multiple times. As with Meme generators they have one line of text at the top and one at the bottom of the image. If it's not possible could anyone let me know or point me in the direction of a tutorial/answer that could let me write on top of an image with text including changing the font and font size plus top/bottom, left/right.
Thanks to anyone who can help.
You can create a function that draws text given your desired styling arguments:
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
drawText('This is the top',canvas.width/2,20,24,'verdana');
drawText('This is the bottom',canvas.width/2,canvas.height-20,16,'Courier');
function drawText(text,centerX,centerY,fontsize,fontface){
ctx.save();
ctx.font=fontsize+'px '+fontface;
ctx.textAlign='center';
ctx.textBaseline='middle';
ctx.fillText(text,centerX,centerY);
ctx.restore();
}
body{ background-color: ivory; }
#canvas{border:1px solid red; margin:0 auto; }
<canvas id="canvas" width=300 height=300></canvas>
Related
I am using html canvas for resizing image.when i am loading image from project folder ,I am passing image path(img/tom.jpg)to canvas.draw method.After that i am getting base64url and then i am appending that url to the img src,That's working perfectly.But when i am picking image in cordova with cordovaImagepicker and then i am passing response url to canvas draw method ,i am getting base64 url but when i am appending that url to img src ,its coming totally black as square.Please help me ..
Here is my html(here in dummyImage {{this is image url that i picked up from device gallery}}):-
<img id="sourceImage" src="" style="display:none;">
<img id="dummyImage" width="150" height="150" src="{{image}}" alt="The Scream" style="display:none;">
<canvas id="myCanvas" width="230" height="230"style="display:none;">
<script>
$(document).ready(function(){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var img = document.getElementById("dummyImage");
ctx.drawImage(img, 0,0, 230, 230);
$('#sourceImage').attr('src', canvas.toDataURL("image/jpeg"));
$.getScript('js/test.js');
});
</script>
Here is my console where in dummyImage(img tag id),i am getting picked image url and in sourceImage i am getting canvas resized url and see i am getting black image...
Your problem probably lies in using src="file:///data/data/...". The source should be something like /path/to/picture.jpg.
The canvas itself is not a black square, that's just the image rendering of an empty/transparent canvas. To confirm this, just remove display:none from the canvas element.
Fiddle: https://jsfiddle.net/u857pehs/1/
In the Javascript portion of The Complete Web Developer Course on Udemy, Rob (the teacher) makes a div randomly become either a circle or square based on a simple if-then statement. But how would I create a square with a completely random border radius?
function getRadius() {
var radius=Math.random();
radius = radius * 100;
}
function makeBox() {
var time=Math.random();
time=time*2000;
setTimeout(function() {
document.getElementById("box").style.borderRadius="50px";
document.getElementById("box").style.backgroundColor=getRandomColor();
document.getElementById("box").style.display="block";
createdTime=Date.now();
}, time);
}
The program works when I have just have an integer with px after like in the example, but I can't seem to figure out how to change the 50 to the function getRadius, if that makes sense. Nothing appears with
html:
<div id="box" onclick="change();">
<p>click me</p>
<div>
css:
#box{
height:100px;
width:100px;
background-color:red;
}
js:
function change(){
var num = Math.floor(Math.random() * 100);
box.style.borderRadius= num+'px';
};
I made canvas like this ,
<canvas id="mainCanvas" style="width:310px;height:212px;">
</canvas>
then try to put png on the canvas in my script
var canvas=document.getElementById("mainCanvas");
var context = canvas.getContext("2d");
var img= new Image();
img.src = "img/player.png";
context.drawImage(img,0,0,310,212);
my plyer.png size is 312 *212 , the same size as canvas size.
However,my png file is expansioned on the canvas. Consequently, right edge and bottom edge protrude from the canvas.
Why my png doesn't fit in ??
All canvas should have width and height attributes specified
<canvas width="310" height="212"></canvas>
Width and height shouldn't use 'px'.
With javascript use:
var c = document.querySelector('canvas');
c.width=310;
c.height=212;// as Ken Fyrstenberg mentioned
Also be sure to wait till image will load.
img.onload(function(){
//drawimage
});
You need to apply the size as an attribute not styles. Try this:
<canvas id="mainCanvas" width="310" height="212">
</canvas>
I've been doing a lot of research into Canvas and SVG, as well as CSS3 3d transforms, and I want to use them all together, and I'm having difficulty getting it all working. I've got an SVG element containing an image-based mask as well as a foreignObject element containing some HTML I'd like to mask, particularly, a canvas. Depending on what I do with the canvas, I'm getting different results in different browsers, and usually not what I want. Applying typical canvas operations, such as rect(), and applying typical css transformations, such as rotate3d(), will, in most browsers, cause the SVG image mask to no longer be applied to the canvas, and the entire canvas will be visible, including the masked part.
Safari (desktop): If the canvas has ever been drawn on or transformed in any way (even, for example, rotate3d(1,1,0,0deg)), it will not be masked.
Safari (iOS 7): If the canvas has ever been drawn on or transformed in any way, it will not be masked.
Chrome (desktop): Here, we can draw to the canvas and it will still be masked, but if it has been transformed, it will not be masked.
Chrome (iOS 7): If the canvas has been drawn on or transformed at all, it will not be masked.
Firefox: No problems here; it will still be masked even if it has been drawn on and transformed.
IE and others: I haven't checked yet.
You can see this illustrated by applying the following code, which is also hosted here, where I set up an example page: http://kage23.com/masktest.html
<svg width="748" height="421" baseProfile="full" version="1.2">
<defs>
<mask id="myMask" transform="scale(1)">
<image width="100%" height="421" xlink:href="http://i.imgur.com/ORtP2fW.png" />
</mask>
</defs>
<foreignObject id="foreignObject" width="100%" height="421">
<div style="background-color: green; width: 100%; height: 421px;">
<p id="myParagraph" style="padding-top: 100px; padding-left: 200px;">Some text or whatever.</p>
<canvas id="effectCanvas" width="500px" height="100px" style="background-color: red;"></canvas>
</div>
</foreignObject>
</svg>
<button onclick="applyMask()">Apply mask</button>
<button onClick="drawToCanvas()">Draw to canvas</button>
<button onClick="rotateCanvas()">Apply rotation to canvas</button>
<script>
var applyMask = function ()
{
var element = document.getElementById('foreignObject');
element.style.mask = 'url(#myMask)';
};
var drawToCanvas = function ()
{
var canvas = document.getElementById('effectCanvas');
var ctx = canvas.getContext('2d');
ctx.fillStyle = "rgb(200,0,0)";
ctx.fillRect (10, 10, 55, 50);
};
var rotateCanvas = function ()
{
var canvas = document.getElementById('effectCanvas');
canvas.style.webkitTransform = 'rotate3d(1,1,1,45deg)';
canvas.style.MozTransform = 'rotate3d(1,1,1,45deg)';
};
</script>
Is there a better way to do this? I'm not committed to using an SVG mask necessarily; I just need to be able to mask a transforming canvas that is also being actively drawn on. Originally, I was using -webkit-mask-box-image, but that's not supported very widely at all.
I couldn't find answers for html5 canvas. Can I change the coordinates of the origin, so that my grafics will move altogether to the right for 15px (just an example). Given the following html and css?
<canvas id="g" width="auto" height="auto">Your browser doesnt support canvas tag</canvas>
css:
canvas, section {
display: block;
}
#g {
position: absolute;
width:100%;
height:100%;
overflow:hidden;
}
Sounds like you need a translate transform. Depending on what else you're doing with the context, you may also need to call save() and restore().
var ctx = document.getElementById('myCanvas').getContext('2d');
ctx.save();
ctx.translate(15, 0);
// ... do stuff with the transformed origin
ctx.restore();
// ... do stuff with the canvas restored to its original state (if necessary)