无法使用选定的颜色进行绘制
Unable to draw using selected colour
描述:我在我的页面上有一个画布绘图,这里是编码。我想让用户选择颜色和绘制。我已经有颜色选择了。
问题:无法使用选定的颜色绘制。我没能算出这个函数。请帮助。
注。通过单击按钮,画布将弹出。
Demo: jsfiddle
<button onClick="openPopup();">click here</button>
<div id="test" class="popup">
<div></div>
<div class="cancel" onclick="closePopup();"></div>
<canvas id="canvas1" width="750" height="720" style="border: 1px solid black">
</canvas>
<p> </p>
<p>
<input type="button" id="Orange" style="background-color: orange; width: 25px;
height: 25px;"/>
<input type="button" id="Yellow" style="background-color: yellow; width: 25px;
height: 25px;"/>
<input type="button" id="Green" style="background-color: green; width: 25px;
height: 25px;"/>
<input type="button" id="Blue" style="background-color: blue; width: 25px;
height: 25px;"/>
<input type="button" id="Purple" style="background-color: purple; width: 25px;
height: 25px;"/>
<input type="button" id="Brown" style="background-color: brown; width: 25px;
height: 25px;"/>
<input type="button" id="Black" style="background-color: black; width: 25px;
height: 25px;"/>
<input type="button" id="White" style="background-color: white; width: 25px;
height: 25px;"/>
</p>
<p><input type="button" id="reset_image" value="Reset Drawing"/></p>
</div>
<style>
#canvas1 {
left:0; /* adjust as needed */
top:0;
}
.popup{
position:absolute;
top:0px;
left:0px;
margin:0px;
width: 900px;
height: 750px;
font-family:verdana;
font-size:13px;
background-color:white;
border:2px solid grey;
z-index:100000000000000000;
display:none;
opacity:0.6;
filter:alpha(opacity=60);
margin-left: 300px;
margin-top: 90px;
overflow: auto;
}
.cancel{
display:relative;
cursor:pointer;
margin:0;
float:right;
height:10px;
width:14px;
padding:0 0 5px 0;
background-color:red;
text-align:center;
font-weight:bold;
font-size:11px;
color:white;
border-radius:3px;
z-index:100000000000000000;
}
.cancel:hover{
background:rgb(255,50,50);
}
</style>
<script>
function openPopup() {
var p = document.getElementById('test');
p.style.display = 'block';
canvas.width = parseInt(p.style.width, '10'); //only when you use pixels
canvas.height = parseInt(p.style.height, '10');
}
function closePopup() {
document.getElementById('test').style.display = 'none';
}
var can = document.getElementById('canvas1');
var ctx = can.getContext('2d');
var isPressed = false;
var mx = 4, my = 4;
function move(e) {
getMouse(e);
if (isPressed) {
ctx.lineTo(mx, my);
ctx.stroke()
}
}
function up(e) {
getMouse(e);
isPressed = false;
}
function down(e) {
getMouse(e);
ctx.beginPath();
ctx.moveTo(mx, my);
isPressed = true;
}
can.onmousemove = move;
can.onmousedown = down;
can.onmouseup = up;
// way oversimplified:
function getMouse(e) {
var element = can, offsetX = 0, offsetY = 0;
mx = e.pageX - 305;
my = e.pageY - 95;
}
</script>
只需将此函数添加到按钮的每个onclick事件中:-
function choosecolor(cps) {
ctx.strokeStyle = cps; // choosen color
}
为每个按钮添加一个onclick事件,像这样:-
<input type="button" onclick="choosecolor('yellow color code')" id="Yellow" style="background-color: yellow; width: 25px;
height: 25px;"/>
查看这个工作小提琴:-小提琴
相关文章:
- 用图像而不是颜色填充对象(将图像绘制到画布上)
- 在画布上绘制两个不同颜色的形状
- 通过 sql 数据绘制高图表柱形图颜色
- ASP.NET 或Javascript库,用于在美国地图上以颜色绘制自定义零件
- 更改 svg 填充颜色,然后绘制到画布
- imagemapster用不同的颜色绘制不同的区域
- 使用特定颜色的li元素进行绘制
- 使用谷歌地图api根据数据中的值绘制不同颜色的圆圈
- HTML5画布绘制像素的颜色与提供的颜色不同
- d3.js在从对象数组中绘制任意数量的线时的唯一颜色
- 无法使用选定的颜色进行绘制
- 在JavaScript中快速绘制/更新二维正方形数组颜色的方法
- 使用WebGL绘制不同颜色的线条
- jQuery/JS图形-根据条件绘制不同颜色的线
- 在javascript中用两种不同的颜色绘制两辆自行车(tronlight)
- 在画布上用随机颜色绘制圆
- EXTJS:如何根据响应中的a属性在TreeGrid中绘制不同颜色的行
- 如何使用SVG图形绘制RGB颜色选择器梯度
- 使用HTML5 Canvas绘制透明度乘以颜色的图像
- 在地图上用不同的颜色绘制多条折线