SVG: круг - квадрат - овал (бесконечно)
Привет. Написал код, чтобы при клике появлялся новый круг, квадрат, овал. Не доходят руки сделать до идеала.
Идеал: В HTML виден только круг, при клике на круг он движется и появляется квадрат в другом месте, при клике на квадрат он движется и появляется овал, на овал - круг (так бесконечно). Чтобы клонировались, с каждым разом их было больше и больше. HELP :D Что у меня: Находятся все 3 фигуры, все двигаются и при клике появляются новые. <!DOCTYPE HTML> <html> <head> <title>Перемещение круга</title> <style> .svgcircle { position: absolute; } .circle { -webkit-transition: all 10s cubic-bezier(0, 0, 1, 1); -moz-transition:all 10s cubic-bezier(0, 0, 1, 1); -o-transition:all 10s cubic-bezier(0, 0, 1, 1); transition:all 10s cubic-bezier(0, 0, 1, 1); } .rect { -webkit-transition: all 10s cubic-bezier(0, 0, 1, 1); -moz-transition:all 10s cubic-bezier(0, 0, 1, 1); -o-transition:all 10s cubic-bezier(0, 0, 1, 1); transition:all 10s cubic-bezier(0, 0, 1, 1); } .ellipse { -webkit-transition: all 10s cubic-bezier(0, 0, 1, 1); -moz-transition:all 10s cubic-bezier(0, 0, 1, 1); -o-transition:all 10s cubic-bezier(0, 0, 1, 1); transition:all 10s cubic-bezier(0, 0, 1, 1); } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> </head> <body> <script> function randomRGBComponent() { return Math.round(Math.random() * 255); } function randomRGBColor() { return 'rgb(' + randomRGBComponent() + ', ' + randomRGBComponent() + ', ' + randomRGBComponent() + ')'; } $("html").on("click", ".circle" , function() { this.setAttribute("cx",Math.random()*500); this.setAttribute("cy",Math.random()*500); $(this).clone().appendTo(".svgcircle"). attr("fill",randomRGBColor()); }) $("html").on("click", ".rect" , function() { this.setAttribute("x",Math.random()*500); this.setAttribute("y",Math.random()*500); $(this).clone().appendTo(".svgcircle"). attr("fill",randomRGBColor()); }) $("html").on("click", ".ellipse" , function() { this.setAttribute("cx",Math.random()*500); this.setAttribute("cy",Math.random()*500); $(this).clone().appendTo(".svgcircle"). attr("fill",randomRGBColor()); }) </script> <svg width ="600" height ="600" class="svgcircle"> <circle class="circle" cx="35" cy="25" r="35" fill="red"/></circle> <rect class="rect" x="50" y="50" width="35" height="35" fill="green" /></rect> <ellipse class="ellipse" cx="25" cy="25" rx="50" ry="25"/> </svg> </body> </html> |
Легкое же :с
Мне только тяжеловато:cray: |
Eadweard,
<!DOCTYPE HTML> <html> <head> <title>Перемещение круга</title> <style> .svgcircle { position: absolute; } .circle { -webkit-transition:all 3s cubic-bezier(0, 0, 1, 1); -moz-transition:all 3s cubic-bezier(0, 0, 1, 1); -o-transition:all 3s cubic-bezier(0, 0, 1, 1); transition:all 3s cubic-bezier(0, 0, 1, 1); } .rect { -webkit-transition: all 3s cubic-bezier(0, 0, 1, 1); -moz-transition:all 3s cubic-bezier(0, 0, 1, 1); -o-transition:all 3s cubic-bezier(0, 0, 1, 1); transition:all 3s cubic-bezier(0, 0, 1, 1); } .ellipse { -webkit-transition: all 3s cubic-bezier(0, 0, 1, 1), opacity 10s linear; -moz-transition:all 3s cubic-bezier(0, 0, 1, 1), opacity 10s linear; -o-transition:all 3s cubic-bezier(0, 0, 1, 1), opacity 10s linear; transition:all 3s cubic-bezier(0, 0, 1, 1), opacity 10s linear; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> </head> <body> <script> function randomRGBComponent() { return Math.round(Math.random() * 255); } function randomRGBColor() { return 'rgba(' + randomRGBComponent() + ', ' + randomRGBComponent() + ', ' + randomRGBComponent() + ',1)'; } $("html").on("click", ".circle", function () { this.setAttribute("cx", Math.random() * 500); this.setAttribute("cy", Math.random() * 500); this.setAttribute('fill',"rgba(255,255,255,0)"); $('rect').attr("fill", randomRGBColor()); }); $("html").on("click", ".rect", function () { this.setAttribute("x", Math.random() * 500); this.setAttribute("y", Math.random() * 500); this.setAttribute('fill', "rgba(255,255,255,0)"); $('ellipse').attr("fill", randomRGBColor()); }); $("html").on("click", ".ellipse", function () { this.setAttribute("cx", Math.random() * 500); this.setAttribute("cy", Math.random() * 500); this.setAttribute('fill', "rgba(255,255,255,0)"); $("circle").attr("fill", randomRGBColor()); }); </script> <svg width ="600" height ="600" class="svgcircle"> <circle class="circle" cx="35" cy="25" r="35" fill="red"/> <rect class="rect" x="200" y="400" width="35" height="35" fill="rgba(0,0,0,0)"/> <ellipse class="ellipse" cx="400" cy="200" rx="50" ry="25" fill="rgba(0,0,0,0)"/> </svg> </body> </html> |
Цитата:
|
Eadweard,
Вариант с клонами. Из-за использования одних и тех же классов и обработчиков есть побочные эффекты. Кроме того, быстро растет объем кода Более "правильный" вариант svg-клонирования - с использованием defs и use - завтра нарисую, сейчас некогда. <!DOCTYPE HTML> <html> <head> <title>Перемещение круга</title> <style> .svgcircle { position: absolute; } .circle { -webkit-transition:all 3s cubic-bezier(0, 0, 1, 1); -moz-transition:all 3s cubic-bezier(0, 0, 1, 1); -o-transition:all 3s cubic-bezier(0, 0, 1, 1); transition:all 3s cubic-bezier(0, 0, 1, 1); } .rect { -webkit-transition: all 3s cubic-bezier(0, 0, 1, 1); -moz-transition:all 3s cubic-bezier(0, 0, 1, 1); -o-transition:all 3s cubic-bezier(0, 0, 1, 1); transition:all 3s cubic-bezier(0, 0, 1, 1); } .ellipse { -webkit-transition: all 3s cubic-bezier(0, 0, 1, 1); -moz-transition:all 3s cubic-bezier(0, 0, 1, 1); -o-transition:all 3s cubic-bezier(0, 0, 1, 1); transition:all 3s cubic-bezier(0, 0, 1, 1); } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script> function randomRGBComponent() { return Math.round(Math.random() * 255); } function randomRGBColor() { return 'rgba(' + randomRGBComponent() + ', ' + randomRGBComponent() + ', ' + randomRGBComponent() + ',1)'; } $("html").on("click", ".circle", function () { this.setAttribute("cx", Math.random() * 500); this.setAttribute("cy", Math.random() * 500); $('rect').clone().appendTo('svg'); var r = $('rect').last(); r.attr("x", Math.random() * 500); r.attr("y", Math.random() * 500); r.attr("fill", randomRGBColor()); r.css("display","block"); }); $("html").on("click", ".rect", function () { this.setAttribute("x", Math.random() * 500); this.setAttribute("y", Math.random() * 500); $('ellipse').clone().appendTo('svg'); var r = $('ellipse').last(); r.attr("cx", Math.random() * 500); r.attr("cy", Math.random() * 500); r.attr("fill", randomRGBColor()); r.css("display","block"); }); $("html").on("click", ".ellipse", function () { this.setAttribute("cx", Math.random() * 500); this.setAttribute("cy", Math.random() * 500); $('circle').clone().appendTo('svg'); var r = $('circle').last(); r.attr("cx", Math.random() * 500); r.attr("cy", Math.random() * 500); r.attr("fill", randomRGBColor()); r.css("display","block"); }); </script> </head> <body> <svg width ="600" height ="600" class="svgcircle"> <circle class="circle" cx="35" cy="25" r="35" fill="red"/> <rect class="rect" x="200" y="400" width="35" height="35" style="display:none"/> <ellipse class="ellipse" cx="400" cy="200" rx="50" ry="25" style="display:none"/> </svg> </body> </html> |
$('rect').clone().appendTo('svg');
За такую строчку в коде нужно долго и больно бить палкой по голове. особенно на ... $("html").on("click", ... |
Цитата:
<!DOCTYPE HTML> <html> <head> <title>Перемещение круга</title> <style type="text/css" > .svgcircle { position: absolute; } .circle { -webkit-transition:all 3s cubic-bezier(0, 0, 1, 1); -moz-transition:all 3s cubic-bezier(0, 0, 1, 1); -o-transition:all 3s cubic-bezier(0, 0, 1, 1); transition:all 3s cubic-bezier(0, 0, 1, 1); } .rect { -webkit-transition: all 3s cubic-bezier(0, 0, 1, 1); -moz-transition:all 3s cubic-bezier(0, 0, 1, 1); -o-transition:all 3s cubic-bezier(0, 0, 1, 1); transition:all 3s cubic-bezier(0, 0, 1, 1); } .ellipse { -webkit-transition: all 3s cubic-bezier(0, 0, 1, 1); -moz-transition:all 3s cubic-bezier(0, 0, 1, 1); -o-transition:all 3s cubic-bezier(0, 0, 1, 1); transition:all 3s cubic-bezier(0, 0, 1, 1); } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script> function randomRGBComponent() { return Math.round(Math.random() * 255); } function randomRGBColor() { return 'rgb(' + randomRGBComponent() + ', ' + randomRGBComponent() + ', ' + randomRGBComponent() + ')'; } $("html").on("click", ".circle", function () { this.setAttribute("cx", Math.random() * 500); this.setAttribute("cy", Math.random() * 500); this.setAttribute("fill", randomRGBColor()); var r = document.createElementNS("http://www.w3.org/2000/svg", "rect"); r.setAttribute("class", "rect"); r.setAttribute("x", Math.random() * 500); r.setAttribute("y", Math.random() * 500); r.setAttribute("width", "35"); r.setAttribute("height", "35"); r.setAttribute("fill", randomRGBColor()); $('svg').append(r); }); $("html").on("click", ".rect", function () { this.setAttribute("x", Math.random() * 500); this.setAttribute("y", Math.random() * 500); this.setAttribute("fill", randomRGBColor()); var r = document.createElementNS("http://www.w3.org/2000/svg", "ellipse"); r.setAttribute("class", "ellipse"); r.setAttribute("cx", Math.random() * 500); r.setAttribute("cy", Math.random() * 500); r.setAttribute("rx", "50"); r.setAttribute("ry", "25"); r.setAttribute("fill", randomRGBColor()); $('svg').append(r); }); $("html").on("click", ".ellipse", function () { this.setAttribute("cx", Math.random() * 500); this.setAttribute("cy", Math.random() * 500); this.setAttribute("fill", randomRGBColor()); var r = document.createElementNS("http://www.w3.org/2000/svg", "circle"); r.setAttribute("class", "circle"); r.setAttribute("cx", Math.random() * 500); r.setAttribute("cy", Math.random() * 500); r.setAttribute("r", "35"); r.setAttribute("fill", randomRGBColor()); $('svg').append(r); }); </script> </head> <body> <svg version="1.1" baseProfile="full" width ="600" height ="600" class="svgcircle"> <circle class="circle" cx="300" cy="300" r="35" fill="red"/> </svg> </body> </html> |
Dilettante_Pro,
может все атрибуты в один обьект и attr? |
рони,
Ну это уже пусть ТС смотрит - я решал в принципе, поэтому все по складАм, для наглядности |
Dilettante_Pro,
от setAttribute рябит :) |
Часовой пояс GMT +3, время: 12:34. |