【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 ...
随机推荐
- mpu
#include "mpu.h" #include "mem.h" #include "my_errno.h" #include " ...
- java知识巩固
1.从控制台读取一个字符: public static void main(String args[]) throws java.io.IOException{ char c=(char)System ...
- Fibonacci(斐波那契)递归实现。容易看懂
#include<iostream>using namespace std;int fibonacci(int n){if(n<=0) return 0; else if(n==1) ...
- 不经过 App store 的安装方式(转)
所有安装到真机(非越狱)的应用(可以是 .app ,也可以是 .ipa ,只要编译时选的是编译成 Arm 的就好..app 转 .ipa 只需要一条命令) 都必须经过证书签名.证书主要有三大种: 企业 ...
- 《Pro Express.js》学习笔记——Express服务启动常规七步
Express服务启动常规七步 1. 引用模块 var express=require('express'), compression=require('compression'), bo ...
- serialize和unserialize函数
序列化是将变量转换为可保存或传输的字符串的过程:反序列化就是在适当的时候把这个字符串再转化成原来的变量使用.这两个过程结合起来,可以轻松地存储和传输数据,使程序更具维护性.1. serialize和u ...
- failed to open the runspace pool. the server manager winrm plug-in might be corrupted or missing
添加对127.0.0.1的监听 netsh http add iplisten 127.0.0.01 添加完后的效果
- php 的txt操作,加入类容
<?php $fr=fopen("./data/test.txt",'a'); //fopen(位置,打开方式) if(!$fr) { echo " error&q ...
- 作业七:团队项目——Alpha版本冲刺阶段003
今日进展:我们的目标是做一款扫雷游戏,所以我们先去玩了几款游戏,找到了扫雷游戏的一些特点. 今日安排:先进行了一些必要的游戏过程,进行了基本的扫雷界面规划.
- Djanog结合jquery实现ajax
最近想在使用django的基础上通过jquery实现页面局部刷新的功能,研究了两天,终于是解决了这个问题,下面把方法步骤记录下来,以备以后重用. 在项目中通过两种形式实现了ajax: 第一种方法:we ...