Jump to content
sEnte

Canvas para desenho

Recommended Posts

sEnte

Estou aqui a tentar criar um canvas en HTML5 para desenhar e tal mas estou com um problema.

no main.js tenho isto

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

var radius = 10;

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

var putPoint = function(e){
context.beginPath();
context.arc(e.clientX, e.clientY, radius, 0, Math.PI*2);
context.fill();
}

canvas.addEventListener('mousedown', putPoint);

index.html

<!doctype html>
<html>
<head>
 <title>Canvas Drawing App</title>
 <meta charset="UTF-8">
</head>
<body style='margin: 0;'>
 <canvas id="canvas" style="display: block;">
  Sorry, your browser is rubbish.
 </canvas>
 <script src="main.js"></script>
</body>
</html>

no entanto não aparece o ponto que deveria aparecer sempre que carrego no rato :S


"If It Ain't Broke, Break it and build something Cooler!" Unknown

Share this post


Link to post
Share on other sites
HappyHippyHippo

só deverás executar o código javascript depois da página estar carregada:

function bodyonload() {
	var canvas = document.getElementById('canvas');
	var context = canvas.getContext('2d');

	var radius = 10;

	canvas.width = window.innerWidth;
	canvas.height = window.innerHeight;

	canvas.addEventListener('mousedown', function(e){
		context.beginPath();
		context.arc(e.clientX, e.clientY, radius, 0, Math.PI*2);
		context.fill();
	});
}

<!DOCTYPE html>
<html>
<head>
 <title>Canvas Drawing App</title>
 <meta charset="UTF-8">
 <script src="main.js"></script>
</head>
<body style='margin: 0;' onload="bodyonload();">
 <canvas id="canvas" style="display: block;">
  Sorry, your browser is rubbish.
 </canvas>
</body>
</html>

  • Vote 1

IRC : sim, é algo que ainda existe >> #p@p

Share this post


Link to post
Share on other sites
sEnte

Agora funciona mas surjiu outro problema :S Agora a ideia é fazer uma linha e mudei o mousedown para mousemove. o problema é que agora está sempre a "pintar" mesmo que eu nem sequer carregue no rato :S

EDIT: Parece que já o resolvi :)

Edited by sEnte

"If It Ain't Broke, Break it and build something Cooler!" Unknown

Share this post


Link to post
Share on other sites
sEnte

Visto ser no mesmo vou continuar com este tópico

Agora estou a tentar fazer com que depois de fazer um desenho qualquer clico no botão Save e ele apareça um pop-up com a imagem que gravou para a pasta images, mas muito sinceramente desconfio que poderá ser um problema de permissões porque não há erros a ser mostrados.

Aqui fica o save.js

var saveButton = document.getElementById('save');
saveButton.addEventListener('click', saveImage);
function saveImage() {
var data = canvas.toDataURL();
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
	if (request.readyState == 4 && request.status == 200) {
		var response = request.responseText;
		window.open('download.php?file=' + response, '_blank', 'location=0, menubar=0');
	}
}
request.open('POST', 'save.php', true);
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
request.send('img=' + data);
}

Aqui fica o conteúdo do save.php e download.php

<?php
$data = $_POST['img'];
$data = str_replace('data:image/png;base64,', '', $data);
$data = str_replace(' ', '+', $data);
$img = base64_decode($data);
$path = 'images/' . uniqid() . '.png';
if (file_put_contents($path, $img)) {
 print $path;
} else {
 header("HTTP/1.1 500 Internal Server Error");
}
?>

<?php
$file = $_GET['file'];
header('Content-type: image/png');
header("Content-disposition: attachment; filename = canvasoutput.png");
readfile($file);
?>

Edited by sEnte

"If It Ain't Broke, Break it and build something Cooler!" Unknown

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.