innerHtml='' out of memory and canvas always take memory - javascript

This is one test,I click the button named paint once,the memory grow more,faster and faster,how can I delete the canvas from memory,($('#div').empty() does not work)if it can not be resolved,I can display it,but every canvas will be biger and biger,because I paint always,can you help me?help!!!
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="aa" style="position: relative;width: 500px;height: 500px;background-color: #048ca7;z-index: 0">
</div>
<script>
var i=500;
var uu=new Array();
var ii=function(){
var c=document.getElementById('aa');
c.innerHTML='';
var num1,num2;
var canvas;
var ctx;
var str='';
for(var j=0;j<=i;j++){
num1=Math.random();
num2=Math.random();
str+=("<canvas id='"+j+"' width='100' height='100' style='margin-top:"+num1*400+"px;margin-left:"+num2*400+"px;position: absolute;z-index:1'></canvas>");
}
c.innerHTML=str;
for(j=0;j<=i;j++){
canvas=document.getElementById(j.toString());
ctx=canvas.getContext('2d');
ctx.strokeStyle='black';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(10,10);
ctx.lineTo(80,80);
ctx.stroke();
}
}
</script>
<button style="width: 100px;height: 20px;margin-top: 20px" onclick="ii()">paint</button>
</body>
</html>

Related

How to turn an html canvas into an image and then draw that canvas onto the canvas

I'm currently working on an html canvas javascript game, and it's just about finished, but I want to scale up the canvas so the whole game is bigger. I tried using something like
var imgData = ctx.getImageData(0, 0, 300, 500);
ctx.putImageData(imgData,0,0,0,0,360,600)
but using this method I couldn't increase the size of the image. What is the best method for this?
There are multiple ways you can achieve this, the two that i've experimented with would use 2 seperate canvases, putImageData is a little weird, and does not work well for scaling up, what i would recommend, is have a separate canvas, the size of the scaled up version, and then use ctx.drawImage to draw the first canvas on the scaled up version:
var c = document.getElementById('c');
var ctx = c.getContext('2d');
var c2 = document.getElementById('c2');
var ctx2 = c2.getContext('2d')
ctx.fillStyle = "red";
ctx.fillRect(10,10,100,100)
var scaleX = c2.width / c.width;
var scaleY = c2.height / c.height;
ctx2.drawImage(c,0,0,c2.width,c2.height)
canvas{
border: 2px solid black
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<canvas id="c" width="200" height="200"></canvas>
<canvas id="c2" width="400" height="400" ></canvas>
<script src="script.js"></script>
</body>
</html>
The other way is to just scale up the context, then draw the image at 0,0, with no width or height specified:
var c = document.getElementById('c');
var ctx = c.getContext('2d');
var c2 = document.getElementById('c2');
var ctx2 = c2.getContext('2d')
ctx.fillStyle = "red";
ctx.fillRect(10,10,100,100)
var scaleX = c2.width / c.width;
var scaleY = c2.height / c.height;
ctx2.scale(scaleX,scaleY)
ctx2.drawImage(c,0,0)
ctx2.scale(1/scaleX,1/scaleY)
canvas{
border: 2px solid black
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<canvas id="c" width="200" height="200"></canvas>
<canvas id="c2" width="400" height="400" ></canvas>
<script src="script.js"></script>
</body>
</html>
One more option is to use the css scale() tag, but that messes with mouse clicks, and is really annoying so i wouldn't do that. Keep in mind, all methods that scale up the canvas (unless your scaling up coordinates and redrawing), will result in blur.
Hope this helps :D

Can't change color on an image drawing canvas

I have created an image drawing canvas but the drawing color can't change. maybe it's javascript set color but can't find. if anyone knows please help me. this is the demo link
http://affibe.com/erevo/
HTML Code:
`
<html class="gr__adsterra_com">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="js/jquery-2.2.4.min.js"></script>
<script type="text/javascript" src="js/pixi.min.js"></script>
<script type="text/javascript" src="js/animationFrame.js"></script>
<script type="text/javascript" src="js/nodes.js"></script>
<style>body{margin: 0; background-color: transparent;}</style>
</head>
<body data-gr-c-s-loaded="true">
<header>
<script>lowPerformance=function (){var ua=navigator.userAgent.toLowerCase(); if (ua.indexOf("iphone") !=-1 || ua.indexOf("mobile") !=-1) return true; if ($(window).width()==0) return; return ($(window).width() <=480) ? true : false;}; var ie=false; var w=window, d=document, documentElement=d.documentElement, body=d.getElementsByTagName('body')[0], height=w.innerHeight|| documentElement.clientHeight|| body.clientHeight; if (navigator.appName=='Microsoft Internet Explorer') ie=true; Nodes.multipleInit([{"post_name": "AFFIBE", "drawnImage": (height>860)?"http:\/\/affibe.com\/erevo\/images\/main\/logo_header_er.png":"http:\/\/affibe.com\/erevo\/images\/main\/logo_header_er.png", "linkTitle": "AFFIBE", "particleDensity": ie?"11":"7", "particleWidth": "0.5", "particleHeight": "0.5"}]); </script>
</header>
</body>
</html>
pixi.min.js:
http://affibe.com/erevo/js/pixi.min.js
animationFrame.js
!function(){for(var n=0,i=["ms","moz","webkit","o"],e=0;e<i.length&&!window.requestAnimationFrame;++e)window.requestAnimationFrame=window[i[e]+"RequestAnimationFrame"],window.cancelAnimationFrame=window[i[e]+"CancelAnimationFrame"]||window[i[e]+"CancelRequestAnimationFrame"];window.requestAnimationFrame||(window.requestAnimationFrame=function(i){var e=(new Date).getTime(),a=Math.max(0,16-(e-n)),o=window.setTimeout(function(){i(e+a)},a);return n=e+a,o}),window.cancelAnimationFrame||(window.cancelAnimationFrame=function(n){clearTimeout(n)})}();
if i understand it the follow code help to you
var c = document.getElementById("canvas");
var ctx= c.getContext("2d");
ctx.fillStyle="#66bfff";
for example
var c = document.getElementById('canvas');
var ctx = c.getContext("2d");
ctx.fillStyle = "#f00";
ctx.fillRect(40, 40, 40, 40);
#canvas{
border: 2px solid #333;
}
<html>
<body>
<canvas id="canvas" width="100" height="100"></canvas>
</body>
</html>

drawImage() with wrong coordinates canvas

i have a problem:
I'm working on map generator, using html canvas, the user inputs X,Y, width and height of an image.
But when i use drawImage with users input, the image doesn't fit the Canvas XY and the select height and width in pixels. Is there anything that I can use to solve this?
<html lang=''>
<head>
<title>Map tools</title>
</head>
<body>
<div align='center'>
<canvas id='map' class='mapcanvas' width="800" height="400">
</canvas>
<p>Send</p>
<textarea id="xmlinput" class="inputTextArea" placeholder="coords"></textarea>
</div>
</body>
<script>
function loadXml(){
co = document.getElementById("xmlinput").value.split(',') // X, Y, H, L
canvas = document.getElementById("map");
context = canvas.getContext("2d");
ground.src = 'http://i.imgur.com/Z3DyMAM.png'
ground.onload = function (){
context.drawImage(ground, co[0], co[1], co[2], co[3]);
}
}
</script>
</html>
Ground was undefined. I've put it in for you.
<html lang=''>
<head>
<title>Map tools</title>
</head>
<body>
<div align='center'>
<canvas id='map' class='mapcanvas' width="800" height="400">
</canvas>
<p>Send</p>
<textarea id="xmlinput" class="inputTextArea" placeholder="coords"></textarea>
<img id='ground' style='display: none' /> <!--You forgot the image!-->
</div>
</body>
<script>
function loadXml(){
var co = document.getElementById("xmlinput").value.split(','), // X, Y, H, L
canvas = document.getElementById("map"),
context = canvas.getContext("2d"),
ground = document.getElementById('ground'); //ground was undefined!
ground.src = 'http://i.imgur.com/Z3DyMAM.png'
ground.onload = function (){
context.drawImage(ground, co[0], co[1], co[2], co[3]);
}
}
</script>
</html>
Your code works perfectly.Just add this to the page(create an image object)
<!DOCTYPE html />
<html lang=''>
<head>
<title>Map tools</title>
</head>
<body>
<div align='center'>
<canvas id='map' class='mapcanvas' width="800" height="400">
</canvas>
<p>Send</p>
<textarea id="xmlinput" class="inputTextArea" placeholder="coords"></textarea>
</div>
</body>
<script>
function loadXml(){
co = document.getElementById("xmlinput").value.split(',') // X, Y, H, L
canvas = document.getElementById("map");
context = canvas.getContext("2d");
var ground=new Image();
ground.src = 'http://i.imgur.com/Z3DyMAM.png'
ground.onload = function (){
context.drawImage(ground, co[0], co[1], co[2], co[3]);
}
}
</script>
</html>

Using a button to draw on Canvas

I have been working on a interactive floorplan, that when you press a button, it would draw on the Canvas element. The more I have been working with this, the more I've grown aware that it might not even be possible - any ideas? Here's how much of the code I have done ( it does not work as intended, obviously)
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<ul id="nav">
<div id="buttons">
<input type="button" id="clear" value="118">
</div>
</ul>
<div id="main">
<canvas id="118" width="1200" height="630" style="border:1px solid #d3d3d3;"></canvas>
<script>
var canvas = document.getElementById("canvas118");
var context = canvas.getContext("2d");
document.getElementById('clear').addEventListener('click', function() {
context.moveTo(87,354);
context.lineTo(169,426);
context.lineTo(277,397);
context.lineTo(198,324);
context.lineTo(87,354);
context.fillstyle = "#ff0000";
context.fill();
context.strokeStyle = "#ff0000";
context.stroke();
});
</script>
</div>
</body>
</html>
Here's your code working / changed:
https://jsfiddle.net/dxhytwf4/2/
created a funcion and added the click event directly on the input:
<input type="button" id="clear" value="118" onclick="draw()" />
Javascript:
function draw() {
var canvas = document.getElementById("118");
var context = canvas.getContext("2d");
...
Also you were getting element canvas118 not 118
Seems to me like you are missing the call to beginPath()...Try it like this:
var c=document.getElementById("canvas118");
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(300,150);
ctx.stroke();
Taken from here.

Image wont load on canvas. canvas.width error

Hey my image wont load onto the canvas. I've been at this for hours and I cant figure out what it is. I started to debug my code in google chrome and the console says canvas.width is set to null. Thats the only error I can find.
Here is my code:
function gameLoop()
{
drawBackground();
drawPlayer();
}
function drawBackground()
{
var img = new Image();
img.src = "Images/GrassTexture.png";
g.drawImage(img,0, 0, 500, 500);
}
function drawPlayer()
{
var player1 = new Image();
player1.src = "Images/Players/Mignolet.png";
g.drawImage(player1,0, 0,50 , 50);
}
setInterval(gameLoop, 1);
var canvas = document.getElementById("Canvas");
canvas.width = 500;
canvas.height = 500;
var g = canvas.getContext("2d");
html
html>
<head>
<title>Football</title>
<link rel="stylesheet" type="text/css" href="Css/Css.css">
</head>
<body>
<script type="text/javascript" src="Js/Js.js"></script>
<div id="Background">
<div id="Header">
</div>
<div id="Wrapper">
<canvas id="Canvas">
</canvas>
</div>
</div>
</body>
</html>
Css
#Canvas
{
border: 1px solid black;
width:500px;
height:500px;
margin-left: 300px
}
the script is being called before the html is rendered which is why you were getting the error. its always best to place scripts just before the <body> tag ends.
<html>
<head>
<title>Football</title>
<link rel="stylesheet" type="text/css" href="Css/Css.css">
</head>
<body>
<div id="Background">
<div id="Header">
</div>
<div id="Wrapper">
<canvas id="Canvas">
</canvas>
</div>
</div>
<script type="text/javascript" src="Js/Js.js"></script>
</body>

Categories