Turtle graphics in SVG?

Is there an equivalent in the svg path to the turtlegraphics logo? instead of the hardcoded x and y coordinates, which also makes me adjust the control points when switching the more relative delta approach. My solution should work in FOCS browsers (Firefox Opera Chrome Safaries ex IE).
Regards Jeroen.

+3
source share
2 answers

1st Google result for SVG path: http://www.w3schools.com/svg/svg_path.asp

Quote: “Capital letters mean absolutely positioned, lower cases mean relatively positioned.”

+4
source

: - : http://forresto.github.com/turtle-svg/


SVG- ( ) JavaScript, :

// Turtle graphics drawing to SVG path
var TAU = 2 * Math.PI;

var pen = true;
var d = "";
var vector = {
  x: 0,
  y: 1
};
var currentAngle = 0;

// Relative turns, angles are 0.0 to 1.0
var turnRight = function(angle){
  currentAngle += angle;
  currentAngle = currentAngle%1;
  vector.x = Math.sin(TAU*currentAngle);
  vector.y = Math.cos(TAU*currentAngle);
};
var turnLeft = function(angle){
  turnRight(-angle);
};

// Absolute turn
var turnTo = function(angle){
  currentAngle = angle;
  vector.x = Math.sin(TAU*currentAngle);
  vector.y = Math.cos(TAU*currentAngle);
};

// Drawing
var penUp = function(){
  pen = false;
};
var penDown = function(){
  pen = true;
};

// Relative moves
var moveForward = function (distance) {
  d += pen ? "l " : "m ";
  d += (distance * vector.x) + " " + (distance * vector.y) + " ";
}

// Absolute moves
var moveTo = function (x, y) {
  d += pen ? "L " : "M ";
  d += x + " " + y + " ";
}

d d. : http://jsfiddle.net/forresto/hVE2U/

+6

Source: https://habr.com/ru/post/1739680/


All Articles