A
Attention, at the end of the message, update!If you're drawing a pencil, it's basically a set of points, or polylinium, just a little step.As I understood the problem with the absorption of this set of points. There's a library like that-- http://mourner.github.io/simplify-js/ it can simplify the set of points in the lazy line.Works on algorithm. https://ru.wikipedia.org/wiki/%D0%90%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC_%D0%A0%D0%B0%D0%BC%D0%B5%D1%80%D0%B0_%E2%80%94_%D0%94%D1%83%D0%B3%D0%BB%D0%B0%D1%81%D0%B0_%E2%80%94_%D0%9F%D0%B5%D0%BA%D0%B5%D1%80%D0%B0 That's an example of his work, drawing a mouse in the canvas area.var canv = document.getElementById('canv'),
ctx = canv.getContext('2d'),
line = [];
var startX = 0,
startY = 0;
function mouseDown(e) {
ctx.clearRect(0, 0, canv.width, canv.height);
startX = e.pageX - e.target.offsetLeft;
startY = e.pageY - e.target.offsetTop;
canv.addEventListener('mouseup', mouseUp);
canv.addEventListener('mousemove', mouseMove);
line = [];
line.push({
x: startX,
y: startY
});
}
function mouseMove(e) {
var x = e.pageX - e.target.offsetLeft,
y = e.pageY - e.target.offsetTop;
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(x, y);
ctx.stroke();
startX = x;
startY = y;
line.push({
x: x,
y: y
});
}
function mouseUp() {
canv.removeEventListener('mouseup', mouseUp);
canv.removeEventListener('mousemove', mouseMove);
aproximate();
}
function aproximate() {
ctx.clearRect(0, 0, canv.width, canv.height);
var res = simplify(line, 5);
console.log(res);
ctx.save();
ctx.strokeStyle = 'red';
ctx.beginPath();
ctx.moveTo(res[0].x, res[0].y);
res.forEach(function(el) {
ctx.lineTo(el.x, el.y);
});
ctx.stroke();
ctx.restore();
}
canv.addEventListener('mousedown', mouseDown);canvas {
border: 1px solid #aaa;
}<script src="http://mourner.github.io/simplify-js/simplify.js"></script>
<canvas id="canv"></canvas>Update: Code outline for mapping similar linesI'm sorry the code wasn't very beautiful. I was in a hurry. var Map = (function() {
ymaps.ready(init);
var myMap;
function init() {
myMap = new ymaps.Map("map", {
center: [57.5262, 38.3061], // Углич
zoom: 11
}, {
balloonMaxWidth: 200,
searchControlProvider: 'yandex#search'
});
addPolygon()
}
function convert(coords) {
var projection = myMap.options.get('projection');
return coords.map(function(el) {
var c = projection.fromGlobalPixels(myMap.converter.pageToGlobal([el.x, el.y]), myMap.getZoom());
return c;
});
}
function addPolygon(coord) {
var myGeoObject = new ymaps.GeoObject({
geometry: {
type: "Polygon",
coordinates: [coord],
},
}, {
fillColor: '#00FF00',
strokeColor: '#0000FF',
opacity: 0.5,
strokeWidth: 3
});
myMap.geoObjects.add(myGeoObject);
}
return {
addPolygon: addPolygon,
convert: convert
};
})();
//----------------------
var canv = document.getElementById('canv'),
ctx = canv.getContext('2d'),
line = [];
var map = document.getElementById('map');
canv.width = map.offsetWidth;
canv.height = map.offsetWidth;
var startX = 0,
startY = 0;
function mouseDown(e) {
ctx.clearRect(0, 0, canv.width, canv.height);
startX = e.pageX - e.target.offsetLeft;
startY = e.pageY - e.target.offsetTop;
canv.addEventListener('mouseup', mouseUp);
canv.addEventListener('mousemove', mouseMove);
line = [];
line.push({
x: startX,
y: startY
});
}
function mouseMove(e) {
var x = e.pageX - e.target.offsetLeft,
y = e.pageY - e.target.offsetTop;
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(x, y);
ctx.stroke();
startX = x;
startY = y;
line.push({
x: x,
y: y
});
}
function mouseUp() {
canv.removeEventListener('mouseup', mouseUp);
canv.removeEventListener('mousemove', mouseMove);
aproximate();
}
function aproximate() {
ctx.clearRect(0, 0, canv.width, canv.height);
var res = simplify(line, 5);
res = Map.convert(res);
Map.addPolygon(res);
}
canv.addEventListener('mousedown', mouseDown);html,
body,
#map {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
canvas {
border: 1px solid #aaa;
position: absolute;
z-index: 99999;
top: 0;
left: 0;
}<canvas id="canv"></canvas>
<div id="map"></div>
<script src="http://mourner.github.io/simplify-js/simplify.js"></script>
<script type="text/javascript" src="https://api-maps.yandex.ru/2.1/?lang=ru_RU"></script>