【HTML5】canvas画布练习
第一步:获取画布元素
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
第二步:绘制
1.正方形
function drawSquare(canvas, context) {
var width = Math.floor(Math.random() * 64); var x = Math.floor(Math.random() * (canvas.width-width));
var y = Math.floor(Math.random() * (canvas.height-width)); context.fillStyle = "lightblue";
console.log(x);
console.log(y);
console.log(width);
context.fillRect(x, y, width, width);
}
2.圆形
function drawSquare(canvas, context) {
var width = Math.floor(Math.random() * 64); var x = Math.floor(Math.random() * (canvas.width-width));
var y = Math.floor(Math.random() * (canvas.height-width)); context.fillStyle = "lightblue";
console.log(x);
console.log(y);
console.log(width);
context.fillRect(x, y, width, width);
}
3.文字
function drawText(canvas, context) {
var selectObj = document.getElementById("foregroundColor");
var index = selectObj.selectedIndex;
var fgColor = selectObj[index].value;
var fontSize = "24"; context.fillStyle = fgColor;
context.font = "bold " + fontSize + "px sans-serif";
context.textAlign = "center"; var message = document.getElementById("message").value;
var x = Math.floor(canvas.width / 2);
var y = Math.floor(canvas.height / 2 - fontSize / 2);
context.fillText(message, x, y);
}
备注:context.textAlign
是设置 fillText()
中 x 坐标指定的是文本哪个部位的坐标,如果设置为 center
, x
就是文本中间的 x 坐标。
4.将画布内容变为图片
function makeImage() {
var canvas = document.getElementById("myCanvas");
canvas.onclick = function() {
window.location = canvas.toDataURL("image/png");
}
}
这部分功能浏览器支持得不够好。
完整代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
*{
-moz-box-sizing: border-box;
box-sizing: border-box;
margin: 0;
padding: 0;
}
main {
width: 640px;
height: 320px;
margin: 32px auto;
text-align: center;
}
canvas {
border: 1px solid #ccc;
}
form section {
margin-bottom: 16px;
}
</style>
</head>
<body>
<main>
<canvas width="640" height="320" id="myCanvas"></canvas>
<form>
<section>
<label for="backgroundColor">Select background :</label>
<select id="backgroundColor">
<option value="white" selected>White</option>
<option value="black">Black</option>
</select>
</section>
<section>
<label for="foregroundColor">Select ForegroundColor:</label>
<select id="foregroundColor">
<option value="white">White</option>
<option value="black" selected>Black</option>
</select>
</section>
<section>
<label for="shape">Select shape:</label>
<select id="shape">
<option value="circles" selected>Circles</option>
<option value="squares">Squares</option>
</select>
</section>
<section>
<label for="message">Input text:</label>
<input type="text" name="message" id="message" maxlength="48">
</section>
<input type="button" value="preview" id="previewButton" onclick="draw(canvas, context);">
</form>
</main>
<script>
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
function drawSquare(canvas, context) {
var width = Math.floor(Math.random() * 64); var x = Math.floor(Math.random() * (canvas.width-width));
var y = Math.floor(Math.random() * (canvas.height-width)); context.fillStyle = "lightblue";
console.log(x);
console.log(y);
console.log(width);
context.fillRect(x, y, width, width);
}
function drawCircle(canvas, context) {
var radius = Math.floor(Math.random() * 48); var x = Math.floor(Math.random() * canvas.width);
var y = Math.floor(Math.random() * canvas.height); context.beginPath();
context.arc(x, y, radius, 0, Math.PI * 2, true);
context.fillStyle = "lightblue";
context.fill();
}
function drawText(canvas, context) {
var selectObj = document.getElementById("foregroundColor");
var index = selectObj.selectedIndex;
var fgColor = selectObj[index].value;
var fontSize = "24"; context.fillStyle = fgColor;
context.font = "bold " + fontSize + "px sans-serif";
context.textAlign = "center"; var message = document.getElementById("message").value;
var x = Math.floor(canvas.width / 2);
var y = Math.floor(canvas.height / 2 - fontSize / 2);
context.fillText(message, x, y);
}
function makeImage() {
var canvas = document.getElementById("myCanvas");
canvas.onclick = function() {
window.location = canvas.toDataURL("image/png");
}
}
function draw(canvas, context) {
drawText(canvas, context);
makeImage();
}
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
*{
-moz-box-sizing: border-box;
box-sizing: border-box;
margin: ;
padding: ;
}
main {
width: px;
height: px;
margin: px auto;
text-align: center;
}
canvas {
border: px solid #ccc;
}
form section {
margin-bottom: px;
}
</style>
</head>
<body>
<main>
<canvas width="640" height="320" id="myCanvas"></canvas>
<form>
<section>
<label for="backgroundColor">Select background :</label>
<select id="backgroundColor">
<option value="white" selected>White</option>
<option value="black">Black</option>
</select>
</section>
<section>
<label for="foregroundColor">Select ForegroundColor:</label>
<select id="foregroundColor">
<option value="white">White</option>
<option value="black" selected>Black</option>
</select>
</section>
<section>
<label for="shape">Select shape:</label>
<select id="shape">
<option value="circles" selected>Circles</option>
<option value="squares">Squares</option>
</select>
</section>
<section>
<label for="message">Input text:</label>
<input type="text" name="message" id="message" maxlength="48">
</section>
<input type="button" value="preview" id="previewButton" onclick="draw(canvas, context);">
</form>
</main>
<script>
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
function drawSquare(canvas, context) {
var width = Math.floor(Math.random() * ); var x = Math.floor(Math.random() * (canvas.width-width));
var y = Math.floor(Math.random() * (canvas.height-width)); context.fillStyle = "lightblue";
console.log(x);
console.log(y);
console.log(width);
context.fillRect(x, y, width, width);
}
function drawCircle(canvas, context) {
var radius = Math.floor(Math.random() * ); var x = Math.floor(Math.random() * canvas.width);
var y = Math.floor(Math.random() * canvas.height); context.beginPath();
context.arc(x, y, radius, , Math.PI * , true);
context.fillStyle = "lightblue";
context.fill();
}
function drawText(canvas, context) {
var selectObj = document.getElementById("foregroundColor");
var index = selectObj.selectedIndex;
var fgColor = selectObj[index].value;
var fontSize = "24"; context.fillStyle = fgColor;
context.font = "bold " + fontSize + "px sans-serif";
context.textAlign = "center"; var message = document.getElementById("message").value;
var x = Math.floor(canvas.width / );
var y = Math.floor(canvas.height / 2 - fontSize / );
context.fillText(message, x, y);
}
function makeImage() {
var canvas = document.getElementById("myCanvas");
canvas.onclick = function() {
window.location = canvas.toDataURL("image/png");
}
}
function draw(canvas, context) {
drawText(canvas, context);
makeImage();
}
</script>
</body>
</html>
【HTML5】canvas画布练习的更多相关文章
- html5——canvas画布
一.基本介绍 1,canvas是画布,可以描画线条,图片等,现代的浏览器大部分都支持. canvas的width,height默认为300*150,要指定画布大小,不能用css样式的widh,heig ...
- html5 canvas画布上合成
source-over 默认.在目标图像上显示源图像. source-atop 在目标图像顶部显示源图像.源图像位于目标图像之外的部分是不可见的. source-in 在目标图像中显示源图像.只有目标 ...
- HTML5 Canvas 画布
一.Canvas是什么? canvas,是一个画布,canvas元素用于在网页上绘制图形. canvas 拥有多种绘制路径.矩形.圆形.字符以及添加图像的方法. 二.创建Canvas元素 加上基本的属 ...
- HTML5 canvas画布写炫彩动态的倒计时效果
html代码如下,插入了2个js代码. <!DOCTYPE html> <html> <head> <title>canvas</title> ...
- css总结19:HTML5 Canvas(画布)
1 <canvas> 标签定义图形,比如图表和其他图像. 例1:简单使用: <canvas id="Canva" width="200" h ...
- Particles.js基于Canvas画布创建粒子原子颗粒效果
文章目录 使用方法 自定义参数 相关链接 Particles.js是一款基于HTML5 Canvas画布的轻量级粒子动画插件,可以设置粒子的形状.旋转.分布.颜色等属性,还可以动态添加粒子,效果非常炫 ...
- 16个富有创意的HTML5 Canvas动画特效集合
HTML5技术正在不断的发展和更新,越来越多的开发者也正在加入HTML5阵营,甚至在移动开发上HTML5的地位也是越来越重要了.HTML5中的大部分动画都是通过Canvas实现,因为Canvas就像一 ...
- html5 canvas 画图移动端出现锯齿毛边的解决方法
使用HTML5的canvas元素画出来的.在移动端手机上测试都发现画图有一点锯齿问题 出现这个问题的原因应该是手机的宽是720像素的, 而这个canvas是按照小于720像素画出来的, 所以在720像 ...
- 16个非常有趣的HTML5 Canvas动画特效集合
HTML5技术正在不断的发展和更新,越来越多的开发者也正在加入HTML5阵营,甚至在移动开发上HTML5的地位也是越来越重要了.HTML5中的大部分动画都是通过Canvas实现,因为Canvas就像一 ...
- HTML5 中的 canvas 画布(一)
---恢复内容开始--- 在HTML5中新添加的元素,canvas 现在支持 IE9+的版本 注意:HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript ...
随机推荐
- checkbox
$(document).ready(function(){ var page_id = {/$page_id/}; var location_id = {/$location_id/}; var lo ...
- 【PCB】扫盲总结
1.PCB是什么 PCB( Printed Circuit Board),中文名称为印制电路板,又称印刷线路板,是重要的电子部件,是电子元器件的支撑体,是电子元器件电气连接的载体.由于它是采用电子印刷 ...
- Eclipse 显示所有文件
Package Explorer -> View Menu -> Filters -> uncheck .* resources http://stackoverflow.com/q ...
- ScrollView can host only one direct child
Android 采用ScrollView布局时出现异常:ScrollView can host only one direct child. 异常原因: 主要是ScrollView内部只能有一个子元素 ...
- PhoneGap初试!
最近公司准备开发一个移动应用,方便起见准备开发web项目,用PhoneGap打包成iOS与Android平台的应用.对PhoneGap完全不了解,所以先装个试下.折腾了大半天,总算弄出点儿眉目,整理下 ...
- activeMq 消费者整合spring
package com.mq.consumer; import javax.jms.JMSException;import javax.jms.Message;import javax.jms.Mes ...
- 获取数据库表详细信息、存储过程、视图、的sql
select s.[name] + '.' + t.[name] as tablename from sys.tables as t,sys.schemas as s where t.schema_i ...
- Trie树-字典查找
描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题: ...
- 事件(event),正则
1.事件(event):事件是可以被 JavaScript 侦测到的行为.网页中的每个元素都可以产生某些可以触发 JavaScript 函数的事件.2.事件源: 触发事件的元素 事件: 被 JavaS ...
- 高精度快速预览打开dwg文件的CAD控件CAD Image DLL介绍及下载
CAD Image DLL对于DXF格式, DWG格式(AutoCAD R12 到AutoCAD 2004/2005), PLT 以及 HPGL/HPGL2文件都有快速的显示速度和精度,开发者再也不会 ...