第一步:获取画布元素

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画布练习的更多相关文章

  1. html5——canvas画布

    一.基本介绍 1,canvas是画布,可以描画线条,图片等,现代的浏览器大部分都支持. canvas的width,height默认为300*150,要指定画布大小,不能用css样式的widh,heig ...

  2. html5 canvas画布上合成

    source-over 默认.在目标图像上显示源图像. source-atop 在目标图像顶部显示源图像.源图像位于目标图像之外的部分是不可见的. source-in 在目标图像中显示源图像.只有目标 ...

  3. HTML5 Canvas 画布

    一.Canvas是什么? canvas,是一个画布,canvas元素用于在网页上绘制图形. canvas 拥有多种绘制路径.矩形.圆形.字符以及添加图像的方法. 二.创建Canvas元素 加上基本的属 ...

  4. HTML5 canvas画布写炫彩动态的倒计时效果

    html代码如下,插入了2个js代码. <!DOCTYPE html> <html> <head> <title>canvas</title> ...

  5. css总结19:HTML5 Canvas(画布)

    1  <canvas> 标签定义图形,比如图表和其他图像. 例1:简单使用: <canvas id="Canva" width="200" h ...

  6. Particles.js基于Canvas画布创建粒子原子颗粒效果

    文章目录 使用方法 自定义参数 相关链接 Particles.js是一款基于HTML5 Canvas画布的轻量级粒子动画插件,可以设置粒子的形状.旋转.分布.颜色等属性,还可以动态添加粒子,效果非常炫 ...

  7. 16个富有创意的HTML5 Canvas动画特效集合

    HTML5技术正在不断的发展和更新,越来越多的开发者也正在加入HTML5阵营,甚至在移动开发上HTML5的地位也是越来越重要了.HTML5中的大部分动画都是通过Canvas实现,因为Canvas就像一 ...

  8. html5 canvas 画图移动端出现锯齿毛边的解决方法

    使用HTML5的canvas元素画出来的.在移动端手机上测试都发现画图有一点锯齿问题 出现这个问题的原因应该是手机的宽是720像素的, 而这个canvas是按照小于720像素画出来的, 所以在720像 ...

  9. 16个非常有趣的HTML5 Canvas动画特效集合

    HTML5技术正在不断的发展和更新,越来越多的开发者也正在加入HTML5阵营,甚至在移动开发上HTML5的地位也是越来越重要了.HTML5中的大部分动画都是通过Canvas实现,因为Canvas就像一 ...

  10. HTML5 中的 canvas 画布(一)

    ---恢复内容开始--- 在HTML5中新添加的元素,canvas 现在支持 IE9+的版本 注意:HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript ...

随机推荐

  1. 高性能 Oracle JDBC 编程

    了解如何利用连接和语句池特性来提高 Oracle 驱动的 JDBC 程序的性能.作者:Yuli Vasiliev2009 年 4 月发布使用诸如连接池和语句池等池技术可以显著提高数据库密集型应用程序的 ...

  2. Node与express开发

    1.初识Express Express 网站上是这样介绍 Express 的: "精简的.灵活的 Node.js Web 程序框架,为构建单页.多页及混合的 Web 程序提供了一系列健壮的功 ...

  3. JAVA动手动脑

    1.运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是 ...

  4. 手机升级到iOS10,用Xcode7.3进行真机调试方法

    今天发布的正式版的iOS10,手机果断升级了,结果发现Xcode7.3不能真机调试了,原因是Xcode7.3里面没有iOS10的sdk,下面这个压缩包你可以下载下来放在你的Xcode7.3里面,当然了 ...

  5. Docker私服仓库push 错误

    docker push 提交时错误提示: docker push 192.168.1.110:5000/redis Error response from daemon: invalid regist ...

  6. iOS GCD NSOperation NSThread等多线程各种举例详解(拷贝)

    2年多的iOS之路匆匆而过,期间也拜读来不少大神的博客,近来突然为自己一直做伸手党感到羞耻,是时候回馈社会.回想当年自己还是小白的时候,照着一些iOS多线程教程学,也只是照抄,只知其然.不知其所以然. ...

  7. Android Build System

    归类一些Android build system 相关的知识. http://elinux.org/Android_Build_System make <local_module> - m ...

  8. 通过InputStream访问文件中的数据的四种方法

    //方法一(每次只读取一个字节) public static void getFile() throws IOException { File file = new File("D:\\a. ...

  9. HDU 1222

    题意: 一头狼和一头兔子在一座山中,给你一个数n表示洞的个数,编号从0~n-1.兔子可以随意躲在其中一个洞中,狼每次都从编号为0的洞出发,接下来走到第m个洞中,问兔子能不能活下来,即不被狼吃掉.例如: ...

  10. 场景4 Data Warehouse Management 数据仓库

    场景4 Data Warehouse Management 数据仓库 parallel 4 100% —> 必须获得指定的4个并行度,如果获得的进程个数小于设置的并行度个数,则操作失败 para ...