问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

html5中怎么画菱形,多边形3

发布网友 发布时间:2023-10-09 09:19

我来回答

2个回答

热心网友 时间:2023-12-09 18:26

一、多边形类:polygon.js

var Point = function (x, y) {
this.x = x;
this.y = y;
};
var Polygon = function (centerX, centerY, radius, sides, startAngle, strokeStyle, fillStyle, filled) {
this.x = centerX;//外接圆心x坐标
this.y = centerY;
this.radius = radius;//外接圆半径
this.sides = sides;//边数
this.startAngle = startAngle;//开始角度
this.strokeStyle = strokeStyle;
this.fillStyle = fillStyle;
this.filled = filled;
};
Polygon.prototype = {
getPoints: function () {//获取多边形所有顶点
var points = [],
angle = this.startAngle || 0;
for (var i=0; i < this.sides; ++i) {
points.push(new Point(this.x + this.radius * Math.sin(angle),
this.y - this.radius * Math.cos(angle)));
angle += 2*Math.PI/this.sides;
}
return points;
},
createPath: function (context) {//创建多边形路径
var points = this.getPoints();
context.beginPath();
context.moveTo(points[0].x, points[0].y);
for (var i=1; i < this.sides; ++i) {
context.lineTo(points[i].x, points[i].y);
}
context.closePath();
},
stroke: function (context) {//对多边形描边
context.save();
this.createPath(context);
context.strokeStyle = this.strokeStyle;
context.stroke();
context.restore();
},
fill: function (context) {//填充
context.save();
this.createPath(context);
context.fillStyle = this.fillStyle;
context.fill();
context.restore();
},
move: function (x, y) {
this.x = x;
this.y = y;
},
};

二、HTML文件

<html>

<head>

<META http-equiv="Content-Type" content="text/html; charset=gbk">

<title>拖动多边形</title>

<style>

body{
background: #eeeeee;
}
#dragDiv {
position: absolute;
left: 25px;
top: 50px;
}

#controls {
position: absolute;
left: 25px;
top: 25px;
}

#canvas {
background: #ffffff;
cursor: crosshair;
margin-left: 10px;
margin-top: 10px;
-webkit-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
-moz-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
}

</style>

</head><body>

<canvas id='canvas' width='850' height='500'>Canvas not supported</canvas>

<div id='controls'>

描边颜色: <select id='strokeStyleSelect'>

<option value='red' selected>red</option>

<option value='green'>green</option>

<option value='blue'>blue</option>

<option value='orange'>orange</option>

<option value='goldenrod'>goldenrod</option>

<option value='navy'>navy</option>

<option value='purple'>purple</option>

</select>

填充颜色: <select id='fillStyleSelect'>

<option value='rgba(255,0,0,0.5)'>semi-transparent red</option>

<option value='green'>green</option>

<option value='orange'>orange</option>

<option value='goldenrod' selected>goldenrod</option>

<option value='navy'>navy</option>

<option value='purple'>purple</option>

</select>

边数: <select id='sidesSelect'>

<option value=4 select>4</option>

<option value=6>6</option>

<option value=8>8</option>

<option value=10>10</option>

<option value=12>12</option>

<option value=20>20</option>

</select>

开始角度: <select id='startAngleSelect'>

<option value=0 select>0</option>

<option value=22.5>22.5</option>

<option value=45>45</option>

<option value=67.5>67.5</option>

<option value=90>90</option>

</select>

是否填充 <input id='fillCheckbox' type='checkbox' checked/>

<input id='eraseAllButton' type='button' value='清除所有'/>

</div>

<div id='dragDiv'>

编辑: <input type='checkbox' id='editCheckbox'/>

</div>

<script src = 'polygon.js'></script>

<script src = 'example.js'></script>

</body></html>

三、JS文件 example.js
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
//清除按钮
eraseAllButton = document.getElementById('eraseAllButton'),
//描边颜色
strokeStyleSelect = document.getElementById('strokeStyleSelect'),
//画多边形的开始角度
startAngleSelect = document.getElementById('startAngleSelect'),
//填充颜色
fillStyleSelect = document.getElementById('fillStyleSelect'),
//不否填充
fillCheckbox = document.getElementById('fillCheckbox'),
//进入编辑
editCheckbox = document.getElementById('editCheckbox'),
//边数
sidesSelect = document.getElementById('sidesSelect'),
//canvas位图数据
drawingSurfaceImageData,

//记录鼠标按下的位置
mousedown = {},
//橡皮筋矩形框
rubberbandRect = {},
dragging = false,//是否在拖动状态
draggingOffsetX,
draggingOffsetY,
sides = 8,
startAngle = 0,
guidewires = true,
editing = false,
//保存已绘制的多边形
polygons = [];
// Functions..........................................................
//画网络线
function drawGrid(color, stepx, stepy) {
context.save()
context.shadowColor = undefined;
context.shadowBlur = 0;
context.shadowOffsetX = 0;
context.shadowOffsetY = 0;

context.strokeStyle = color;
context.fillStyle = '#ffffff';
context.lineWidth = 0.5;
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
context.beginPath();
for (var i = stepx + 0.5; i < context.canvas.width; i += stepx) {
context.moveTo(i, 0);
context.lineTo(i, context.canvas.height);
}
context.stroke();
context.beginPath();
for (var i = stepy + 0.5; i < context.canvas.height; i += stepy) {
context.moveTo(0, i);
context.lineTo(context.canvas.width, i);
}
context.stroke();
context.restore();
}
//窗口坐标转canvas坐标
function windowToCanvas(x, y) {
var bbox = canvas.getBoundingClientRect();
return { x: x - bbox.left * (canvas.width / bbox.width),
y: y - bbox.top * (canvas.height / bbox.height)
};
}
// 保存或恢复已绘制的画面...................................
function saveDrawingSurface() {
drawingSurfaceImageData = context.getImageData(0, 0,canvas.width,canvas.height);
}
function restoreDrawingSurface() {
context.putImageData(drawingSurfaceImageData, 0, 0);
}
// 画多边形.....................................................
function drawPolygon(polygon) {
context.beginPath();
polygon.createPath(context);
polygon.stroke(context);
if (fillCheckbox.checked) {
polygon.fill(context);
}
}
// 更新橡皮筋矩形框...................................................
function updateRubberbandRectangle(loc) {
rubberbandRect.width = Math.abs(loc.x - mousedown.x);
rubberbandRect.height = Math.abs(loc.y - mousedown.y);
if (loc.x > mousedown.x) rubberbandRect.left = mousedown.x;
else rubberbandRect.left = loc.x;
if (loc.y > mousedown.y) rubberbandRect.top = mousedown.y;
else rubberbandRect.top = loc.y;
}
//以鼠标按下点为圆心,橡皮筋框宽为半径创建多边形
function drawRubberbandShape(loc, sides, startAngle) {
var polygon = new Polygon(mousedown.x, mousedown.y,
rubberbandRect.width,
parseInt(sidesSelect.value),
(Math.PI / 180) * parseInt(startAngleSelect.value),
context.strokeStyle,
context.fillStyle,
fillCheckbox.checked);
drawPolygon(polygon);//画多边形

if (!dragging) {//保存这个多边形
polygons.push(polygon);
}
}
//更新橡皮筋
function updateRubberband(loc, sides, startAngle) {
updateRubberbandRectangle(loc);
drawRubberbandShape(loc, sides, startAngle);
}
// 网络线.................................................
function drawHorizontalLine (y) {
context.beginPath();
context.moveTo(0,y+0.5);
context.lineTo(context.canvas.width,y+0.5);
context.stroke();
}
function drawVerticalLine (x) {
context.beginPath();
context.moveTo(x+0.5,0);
context.lineTo(x+0.5,context.canvas.height);
context.stroke();
}
function drawGuidewires(x, y) {
context.save();
context.strokeStyle = 'rgba(0,0,230,0.4)';
context.lineWidth = 0.5;
drawVerticalLine(x);
drawHorizontalLine(y);
context.restore();
}
//绘制保存的所有多边形
function drawPolygons() {
polygons.forEach( function (polygon) {
drawPolygon(polygon);
});
}

希望能够帮助到你!

热心网友 时间:2023-12-09 18:27

使用transform的skew(),例如 transform:skewX(10deg)
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
美的面包机和面要多久 美的面包机和面的时间要多长 美的面包机怎么用手动操作 如何用美的面包机发面 如何用美的面包机和面 美的面包机如何和面 CDR教程—教你如何使用CorelDRAW复制图形方法 苹果手机微信怎么换漂亮字体(苹果手机微信怎么换行输入) 有什么好用的app转换字体 手写转文字的软件 erp可以看评论地址吗 淘宝评论url是什么意思? 苹果手机在保修范围内摄像头坏了可以免费维修吗?33 怎样用Javascript画个实心圆和计算器10 js 怎么设置这个圆的起始位置1 怎么用d3.js画出来三个颜色不同的小球1 苹果手机后置摄像头黑屏怎么办339 如何用javascript画两个圆,找到两个圆心,将圆心连线...2 苹果手机后置摄像头坏啦怎么办45 怎么样才能学好数学和英语,1 15年起亚k5主气囊怎么拆1 知了怎么繁殖下一代61 黄金螺怎么繁殖和怎么饲养?64 怎样让斑马螺繁殖?9 螺蛳怎么繁殖?42 斑马螺怎么繁殖?12 辣螺是怎么繁殖的?17 如何在百度里面评价别人的文章及其他的一些东西,麻烦各位帮我解... ABB510变频器到20HZ左右,电流达到额定后就不能升频到...4 汽车机油加注口一般在哪?736 c#编程!求各位大神帮我解决这个问题,谢谢! 刚装的空调可以加变频器吗?2 求助:北京比较靠谱的实木家具性价比又特别高的,大神们给说几个... 想在淘宝上买一套实木沙发,不知道哪一家店东西比较好,性价比高...1 去哪里购买全实木家具比较好? 买实木家具什么木材好 3类材质性价比高2 想在中山买一套木质的家具,实木家具和红木家具是社么区别啊,还...2 实木家具哪种木头最好110 贴皮与纯实木的区别?怎么辨别纯实木家具81 网上购物现在正变得越来越受欢迎用英语怎么说3 这样我们的地球就会变得更加的美好翻译为英语6 黄昏少女x失忆安魂曲罗马音歌词33 请问2107是什么意思,关于感情的,谢谢。14 你好,卫生间砸了六块30的地砖,防水层以破坏,请问防水怎么做...2 卫生间防水怎么重做?由于卫生间的防水层被弄坏了好几个洞,能否...2 机油尺怎么看?加机油的时候,加到哪就可以了?9 卫生间漏水了,如何修补防水层?8 金立gn8001l如何连接电视机 您好!请问厕所的墙砖打掉了几块,防水破坏,重刷了那几块的防水...3 CF爆破的黑色城镇,新年街区,沙漠灰有哪些传点? 金立gn5001usb如何与电脑连接 卫生间漏水,需要敲开几块砖,防水需要全部重做么4