function windows_init(selector) { window_canvas = $('<canvas></canvas>'). attr('width',window_width). attr('height',window_height). attr('background','blue'). insertAfter(selector); }
function make_leaf(canvas, x,y, width, height, window) { canvas.drawRect({ layer: true, strokeStyle: window_silver, fillStyle: window_blue, strokeWidth: 1, x: x, y: y, width: width, height: height, fromCenter: false, }); }
// window opening draw function open_left(canvas, x, y, width, height) { canvas.drawLine({ strokeStyle: window_gray, strokeWidth: 1, x1: x, y1: y, x2: x + width, y2: y + (height / 2), x3: x, y3: y + height, }); } function open_right(canvas, x, y, width, height) { canvas.drawLine({ strokeStyle: window_gray, strokeWidth: 1, x1: x + width, y1: y, x2: x, y2: y + (height / 2), x3: x + width, y3: y + height, }); } function tilt(canvas, x, y, width, height) { canvas.drawLine({ strokeStyle: window_gray, strokeWidth: 1, x1: x, y1: y + height, x2: x + (width / 2), y2: y, x3: x + width, y3: y + height, }); }
function window_vertical(canvas, x, y, width, height, leafs, window) { var leaf = width / leafs; for (var i = 0; i < leafs; i++) { var leaf_x = x + (leaf * i); var leaf_y = y; var leaf_width = leaf; var leaf_height = height; var leaf_num = i; make_leaf(canvas, leaf_x, leaf_y, leaf_width, leaf_height, window, leaf_num); } }
function window_horisontal(canvas, x, y, width, height, leafs, window) { var leaf = height / leafs; for (var i = 0; i < leafs; i++) { var leaf_x = x; var leaf_y = y + (leaf * i); var leaf_width = width; var leaf_height = leaf; var leaf_num = i; make_leaf(canvas, leaf_x,leaf_y,leaf_width, leaf_height, window, leaf_num); } }
function window_t(canvas, x,y,width, height,leafs, window) { var w = width / leafs; make_leaf(canvas, x, y, width, height / 3, window, 0); for (var i = 0; i < leafs; i++) { var leaf_x = x + (w * i); var leaf_y = y + (height / 3 ); var leaf_width = w; var leaf_height = height * 2 / 3; var leaf_num = i + 1; make_leaf(canvas, leaf_x,leaf_y,leaf_width, leaf_height, window, leaf_num); } }
function windows_catalog() { window_horisontal( window_canvas, 0, padding, catalog_height, catalog_height, 1, {type: 'single', leafs: 1, from: 'catalog'}); var offset = catalog_height + padding; for (var i = 2; i < 5; i++) { window_vertical( window_canvas, offset, padding, catalog_height * (i / 2), catalog_height, i, {type: 'vertical', leafs: i, from: 'catalog'}); offset += padding + (catalog_height * (i / 2)); } window_horisontal( window_canvas, offset, padding, catalog_height, catalog_height, 2, {type: 'horisontal', leafs: 2, from: 'catalog'}); offset += padding + catalog_height; for (var i = 0; i < 3; i++) { window_t( window_canvas, offset, padding, catalog_height, catalog_height, i + 2, {type: 't', leafs: i + 2, from: 'catalog'}); offset += padding + catalog_height } }
function make_leaf(canvas, x,y, width, height, window, leaf_num) { canvas.drawRect({ layer: true, strokeStyle: window_silver, fillStyle: window_blue, strokeWidth: 1, x: x, y: y, width: width, height: height, fromCenter: false, click: function(layer) { leaf_clicked(window, leaf_num) } }); }
function leaf_clicked(window, leaf_num) { if ( ! window) { return; } window_canvas.clearCanvas(); windows_catalog(); if (window.size == 'big') { trigger_opening(leaf_num); } big_window(window.type, window.leafs); }
function opening(canvas, x, y, width, height, num) { switch (window_opening[num]) { case 'left': open_left(canvas, x, y, width, height); break; case 'left tilt': open_left(canvas, x, y, width, height); tilt(canvas, x, y, width, height); break; case 'right': open_right(canvas, x, y, width, height); break; case 'right tilt': open_right(canvas, x, y, width, height); tilt(canvas, x, y, width, height); break; } }
// window opening var window_opening = []; var opening_order = ['none', 'left tilt', 'right tilt', 'left', 'right'];
function set_opening(leaf_count) { for (var i = 0; i < leaf_count; i++) { window_opening.push(opening_order[0]); } }
function trigger_opening(num) { var current = opening_order.indexOf(window_opening[num]); if ((current + 2) > opening_order.length) { current = 0; } else { current++; } window_opening[num] = opening_order[current]; window_data(); }
function window_data() { var string = order.type + '|' + order.leafs; for (var i in window_opening) { string += '|' + window_opening[i]; } var select = $('input[name="window_type"]'); select.val(string); }
function window_from_string(string) { if ( ! string.length) { return; } var data = string.split('|'); for (var i = 0; i < 10; i++) { window_opening[i] = data[i + 2]; } big_window(data[0],data[1]); }
function small_window_from_string(element, string, width, height) { if ( ! string.length) { return; } var small_canvas = $('<canvas></canvas>'). attr('width',width). attr('height',height). appendTo(element); var data = string.split('|'); for (var i = 0; i < 10; i++) { window_opening[i] = data[i + 2]; } var leafs = data[1]; switch (data[0]) { case 'single': window_vertical(small_canvas, 0, 0, width, height, leafs, false); break; case 'vertical': window_vertical(small_canvas, 0, 0, width, height, leafs, false); break; case 'horisontal': window_horisontal(small_canvas, 0, 0, width, height, leafs, false); break; case 't': window_t(small_canvas, 0, 0, width, height, leafs, false); break; } }
function windows_handler() { // add or edit var select = $('input[name="window_type"]'); if (select.length) { select.hide(); windows_init(select); window_from_string(select.val()); } // show small window $('.magic_make_window').each(function() { small_window_from_string($(this),$(this).attr('window'), $(this).width(), $(this).height()) }); }
$(document).ready(function() { set_opening(10); }); function windows_handler() { // add or edit var select = $('input[name="window_type"]'); if (select.length) { select.hide(); windows_init(select); window_from_string(select.val()); } // show small window $('.magic_make_window').each(function() { small_window_from_string($(this),$(this).attr('window'), $(this).width(), $(this).height()) }); } function small_window_from_string(element, string, width, height) { if ( ! string.length) { return; } var small_canvas = $('<canvas></canvas>'). attr('width',width). attr('height',height). appendTo(element); var data = string.split('|'); for (var i = 0; i < 10; i++) { window_opening[i] = data[i + 2]; } var leafs = data[1]; switch (data[0]) { case 'single': window_vertical(small_canvas, 0, 0, width, height, leafs, false); break; case 'vertical': window_vertical(small_canvas, 0, 0, width, height, leafs, false); break; case 'horisontal': window_horisontal(small_canvas, 0, 0, width, height, leafs, false); break; case 't': window_t(small_canvas, 0, 0, width, height, leafs, false); break; } } function window_from_string(string) { if ( ! string.length) { return; } var data = string.split('|'); for (var i = 0; i < 10; i++) { window_opening[i] = data[i + 2]; } big_window(data[0],data[1]); } var window_width = 900; var window_height = 350; var catalog_height = window_width / 18; var padding = 15; var window_canvas; var window_blue = '#8CD3EF'; var window_silver = 'white'; var window_gray = 'black'; var order = {type: undefined, leafs: undefined}; function window_data() { var string = order.type + '|' + order.leafs; for (var i in window_opening) { string += '|' + window_opening[i]; } var select = $('input[name="window_type"]'); select.val(string); } function windows_init(selector) { window_canvas = $('<canvas></canvas>'). attr('width',window_width). attr('height',window_height). attr('background','blue'). insertAfter(selector); windows_catalog(); } function windows_catalog() { window_horisontal( window_canvas, 0, padding, catalog_height, catalog_height, 1, {type: 'single', leafs: 1, from: 'catalog'}); var offset = catalog_height + padding; for (var i = 2; i < 5; i++) { window_vertical( window_canvas, offset, padding, catalog_height * (i / 2), catalog_height, i, {type: 'vertical', leafs: i, from: 'catalog'}); offset += padding + (catalog_height * (i / 2)); } //~ for (var i = 2; i < 6; i++) //~ { window_horisontal( window_canvas, offset, padding, catalog_height, catalog_height, 2, {type: 'horisontal', leafs: 2, from: 'catalog'}); offset += padding + catalog_height; //~ } for (var i = 0; i < 3; i++) { window_t( window_canvas, offset, padding, catalog_height, catalog_height, i + 2, {type: 't', leafs: i + 2, from: 'catalog'}); offset += padding + catalog_height } } function window_t(canvas, x,y,width, height,leafs, window) { var w = width / leafs; make_leaf(canvas, x, y, width, height / 3, window, 0); for (var i = 0; i < leafs; i++) { var leaf_x = x + (w * i); var leaf_y = y + (height / 3 ); var leaf_width = w; var leaf_height = height * 2 / 3; var leaf_num = i + 1; make_leaf(canvas, leaf_x,leaf_y,leaf_width, leaf_height, window, leaf_num); if (window.from != 'catalog') { opening(canvas, leaf_x,leaf_y,leaf_width, leaf_height, leaf_num); } } } function window_vertical(canvas, x, y, width, height, leafs, window) { var leaf = width / leafs; for (var i = 0; i < leafs; i++) { var leaf_x = x + (leaf * i); var leaf_y = y; var leaf_width = leaf; var leaf_height = height; var leaf_num = i; make_leaf(canvas, leaf_x, leaf_y, leaf_width, leaf_height, window, leaf_num); if (window.from != 'catalog') { opening(canvas, leaf_x, leaf_y, leaf_width, leaf_height, leaf_num); } } } function window_horisontal(canvas, x, y, width, height, leafs, window) { var leaf = height / leafs; for (var i = 0; i < leafs; i++) { var leaf_x = x; var leaf_y = y + (leaf * i); var leaf_width = width; var leaf_height = leaf; var leaf_num = i; make_leaf(canvas, leaf_x,leaf_y,leaf_width, leaf_height, window, leaf_num); if (window.from != 'catalog') { opening(canvas, leaf_x,leaf_y,leaf_width, leaf_height, leaf_num); } } } function make_leaf(canvas, x,y, width, height, window, leaf_num) { canvas.drawRect({ layer: true, strokeStyle: window_silver, fillStyle: window_blue, strokeWidth: 1, x: x, y: y, width: width, height: height, fromCenter: false, click: function(layer) { leaf_clicked(window, leaf_num) } }); } function big_window(window_type, leafs) { var padding_top = catalog_height + (padding * 2); if (window_width > window_height) { var segment = window_height - padding_top; } //~ else //~ { //~ var segment = (window_width - catalog_height - (padding * 3)) / 2; //~ } order.type = window_type; order.leafs = leafs; window_data(); switch (window_type) { case 'single': window_vertical( window_canvas, 0, padding_top, segment, segment, leafs, {type: 'single', leafs: 1, size: 'big'}); break; case 'vertical': window_vertical( window_canvas, 0, padding_top, segment /2 * leafs, segment, leafs, {type: 'vertical', leafs: leafs, size: 'big'}); break; case 'horisontal': window_horisontal( window_canvas, 0, padding_top, (segment * 2) / leafs, segment, leafs, {type: 'horisontal', leafs: leafs, size: 'big'}); break; case 't': window_t( window_canvas, 0, padding_top, segment, segment, leafs, {type: 't', leafs: leafs, size: 'big'}); break; } } function leaf_clicked(window, leaf_num) { if ( ! window) { return; } window_canvas.clearCanvas(); windows_catalog(); if (window.size == 'big') { trigger_opening(leaf_num); } big_window(window.type, window.leafs); } function opening(canvas, x, y, width, height, num) { switch (window_opening[num]) { case 'left': open_left(canvas, x, y, width, height); break; case 'left tilt': open_left(canvas, x, y, width, height); tilt(canvas, x, y, width, height); break; case 'right': open_right(canvas, x, y, width, height); break; case 'right tilt': open_right(canvas, x, y, width, height); tilt(canvas, x, y, width, height); break; } } // window opening draw function open_left(canvas, x, y, width, height) { canvas.drawLine({ strokeStyle: window_gray, strokeWidth: 1, x1: x, y1: y, x2: x + width, y2: y + (height / 2), x3: x, y3: y + height, }); } function open_right(canvas, x, y, width, height) { canvas.drawLine({ strokeStyle: window_gray, strokeWidth: 1, x1: x + width, y1: y, x2: x, y2: y + (height / 2), x3: x + width, y3: y + height, }); } function tilt(canvas, x, y, width, height) { canvas.drawLine({ strokeStyle: window_gray, strokeWidth: 1, x1: x, y1: y + height, x2: x + (width / 2), y2: y, x3: x + width, y3: y + height, }); } // window opening var window_opening = []; var opening_order = ['none', 'left tilt', 'right tilt', 'left', 'right']; function set_opening(leaf_count) { for (var i = 0; i < leaf_count; i++) { window_opening.push(opening_order[0]); } } function trigger_opening(num) { var current = opening_order.indexOf(window_opening[num]); if ((current + 2) > opening_order.length) { current = 0; } else { current++; } window_opening[num] = opening_order[current]; window_data(); }
Source: https://habr.com/ru/post/238065/
All Articles