I am working on a project on canvas in Html in which I have to:
- let the user draw a circle by dragging the mouse and it should be colorless.
- and enter the color of the circle from the list of options, but this should be done after creating the circle.
and now I can make empty circles (without colors in them) in the canvas, but there are many drawbacks to this: Let's now skip the drag part
questions:
- I can only make a circle when I move the mouse left-right, if I try to create a circle at the top bottom or vice versa, a different position will not happen
- I can create an empty circle, but I am not able to fill the colors, and if I create a circle and move the mouse back to the starting point without releasing it, it will be created as several circles inside each other.
I can’t think of anything. I put all my code. I hope someone can help me: -
<head>
<title></title>
<script src="C:\Users\marvMG\Documents\HTML\jquery-ui-1.11.3\jquery.color.js"></script>
<script src="C:\Users\marvMG\Documents\HTML\jquery-ui-1.11.3\jquery-1.11.1.min.js"></script>
<script src="C:\Users\marvMG\Documents\HTML\jquery-ui-1.11.3\jquery.easing.1.3.js"></script>
<script src="C:\Users\marvMG\Documents\HTML\jquery-ui-1.11.3\jquery-ui.js"></script>
<style type="text/css">
.clrPicker
{
width: 30px;
height: 30px;
border: 1px solid #808080;
margin-bottom: 5px;
cursor:pointer;
}
</style>
</head>
<body>
<div class="tr0">
<div class="td">
Select Drawing tool : <br />
<input type="radio" name="dTool" id="dToolC" value="Circle" /> <label for="dToolC" onclick="DrawCircle()">Circle</label>
</div>
</div>
<div id="board" style="width: 930px;">
<div>
<canvas id="kfCanvas" width="800px" height="500px;" style="border: 3px dotted #000;cursor:crosshair;">
Sorry, your browser doesn't support canvas technology.
</canvas>
<div style="float: right;">
<div>
Color picker:
<div class="clrPicker" style="background-color:black;" onclick="SetBrushColor('black')"></div>
<div class="clrPicker" style="background-color:red;" onclick="SetBrushColor('red')"></div>
<div class="clrPicker" style="background-color:blue;" onclick="SetBrushColor('blue')"></div>
<div class="clrPicker" style="background-color:green;" onclick="SetBrushColor('green')"></div>
<div class="clrPicker" style="background-color:orange;" onclick="SetBrushColor('orange')"></div>
<div class="clrPicker" style="background-color:yellow;" onclick="SetBrushColor('yellow')"></div>
</div>
</div>
</div>
<script>
var curColor = 'black';
var context;
var startX, startY;
var canvasX, canvasY;
var width, height;
var toolSelected;
var kfCanvas = document.getElementById("kfCanvas");
if (kfCanvas) {
var isDown = false;
ctx = kfCanvas.getContext("2d");
DrawAWhiteBase();
$(kfCanvas).mousedown(function (e) {
isDown = true;
startX = e.pageX - kfCanvas.offsetLeft;
startY = e.pageY - kfCanvas.offsetTop;
toolSelected = $("input[type='radio'][name='dTool']:checked");
}).mousemove(function (e) {
if (isDown != false) {
canvasX = e.pageX - kfCanvas.offsetLeft;
canvasY = e.pageY - kfCanvas.offsetTop;
width = Math.abs(canvasX - startX);
height = Math.abs(canvasY - startY);
var beginrad = startX;
var endrad = canvasX;
var radius = endrad - beginrad;
var toolSelected = $("input[type='radio'][name='dTool']:checked");
DrawCircle(startX, startY, radius);
}
}).mouseup(function (e) {
isDown = false;
ctx.closePath();
});
}
function DrawAWhiteBase() {
ctx.beginPath();
ctx.fillStyle = "white";
ctx.fillRect(0, 0, kfCanvas.width, kfCanvas.height);
ctx.closePath();
}
function DrawCircle(x, y, r) {
ctx.beginPath();
ctx.arc(x, y, r, 0, Math.PI * 2, true);
ctx.stroke();
ctx.closePath();
ctx.fill();
}
</script>
</div>
</body>
source
share