Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Криницын Игорь #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 0 additions & 110 deletions index.html

This file was deleted.

9 changes: 9 additions & 0 deletions snowflake.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<script src="snowflake.js"></script>
<body>
<canvas height="500" width="500" id="snowflake"></canvas>
<input size="10" id="input"></input>
<button style="width:75px;height:20px" name="Start" onclick="run()">Draw</button>
</body>
</html>
81 changes: 81 additions & 0 deletions snowflake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
function drawSnowflake(iteration) {
var canvas = document.getElementById("snowflake");
var canvasHeight = parseInt(canvas.getAttribute("height"));
var canvasWidth = parseInt(canvas.getAttribute("width"));

var len;
if(canvasHeight / canvasWidth <= (Math.sin(Math.PI / 3) * 4 / 3)) {
len = canvasHeight * 3 / (4 * Math.sin(Math.PI / 3));
} else {
len = canvasWidth;
}
var x = 0;
var y = len * Math.sin(Math.PI / 3);
var angle = -60 * Math.PI / 180;

var context = canvas.getContext("2d");

canvas.width = canvas.width;//Очистка канваса
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Делаешь хак - не пиши комментарий, а выдели метод/функцию. Удачная функция: function clearCanvas(canvas);

Кстати, на StackOverflow по поводу этого хака.
Заметь, что context.clearRect понятен без комментария и отдельного метода.

Use: context.clearRect(0, 0, canvas.width, canvas.height);

This is the fastest and most descriptive way to clear the entire canvas.

Do not use: canvas.width = canvas.width;

Resetting canvas.width resets all canvas state (e.g. transformations, lineWidth, strokeStyle, etc.), it is very slow (compared to clearRect), it doesn't work in all browsers, and it doesn't describe what you are actually trying to do.


context.lineWidth = "1";
context.strokeStyle = "#0000FF";

drawSide(iteration, x, y, angle, len, context);

x = x + len * Math.cos(angle);
y = y + len * Math.sin(angle);
angle += 120 * Math.PI / 180;
drawSide(iteration, x, y, angle, len, context);

x = x + len * Math.cos(angle);
y = y + len * Math.sin(angle);
angle += 120 * Math.PI / 180;
drawSide(iteration, x, y, angle, len, context);
}
/**
* Рисует сторону снежинки Коха
*
* @param {Number} iteration Количество итераций
* @param {Number} x Абсцисса
* @param {Number} y Ордината
* @param {Number} angle Угол поворота
* @param {Number} len Длина линии
* @param {object} context HTML context
*/
function drawSide(iteration, x, y, angle, len, context) {
if(iteration > 0) {
drawSide(iteration - 1, x, y, angle, len/3, context);

x = x + len / 3 * Math.cos(angle);
y = y + len / 3 * Math.sin(angle);
angle -= 60 * Math.PI / 180;
drawSide(iteration - 1, x, y, angle, len/3, context);

x = x + len / 3 * Math.cos(angle);
y = y + len / 3 * Math.sin(angle);
angle += 120 * Math.PI / 180;
drawSide(iteration - 1, x, y, angle, len/3, context);

x = x + len / 3 * Math.cos(angle);
y = y + len / 3 * Math.sin(angle);
angle -= 60 * Math.PI / 180;
drawSide(iteration - 1, x, y, angle, len/3, context);
}
else {
var finishX = x + len * Math.cos(angle);
var finishY = y + len * Math.sin(angle);
drawLine(x, y, finishX, finishY, context);
}
}

function drawLine(x, y, finishX, finishY, context) {
context.beginPath();
context.moveTo(x, y);
context.lineTo(finishX, finishY);
context.stroke();
}

function run() {
var iteration = document.getElementById("input").value;
drawSnowflake(iteration);
}