Pavel 7 years ago
parent
commit
be855220f8
7 changed files with 554 additions and 14 deletions
  1. 1 14
      js11XMLHttpRequest/Weather/main.js
  2. 39 0
      js12/canvas/index.css
  3. 30 0
      js12/canvas/index.html
  4. 278 0
      js12/canvas/index.js
  5. 15 0
      js12/emoji.js
  6. 85 0
      js12/index.html
  7. 106 0
      js12/main.js

+ 1 - 14
js11XMLHttpRequest/Weather/main.js

@@ -52,20 +52,7 @@ request.onreadystatechange = function(){ //обработчик изменени
             			var resWeather4 = resWeather3.slice(0,resWeather3.length - 28);
             			weatherDiv.innerHTML = resWeather4;
             			console.log(resWeather4)
-            			// var resObj = obj.query.results;
-            			// var keys = Object.keys(resObj);
-            			// var table = document.createElement("table");
-            			// for (var i = 0; i < keys.length; i++) {
-            			// 	var tr = document.createElement("tr");
-            			// 	var td1 = document.createElement("td");
-            			// 	var td2 = document.createElement("td");
-            			// 	td1.innerHTML = keys[i];
-            			// 	td2.innerHTML = resObj[keys[i]];
-            			// 	tr.appendChild(td1);
-            			// 	tr.appendChild(td2);
-            			// 	table.appendChild(tr);
-            			// }
-            			// document.body.appendChild(table);
+            			
             		}
             		else if(request.readyState > 1 && request.readyState < 4){
             			weatherDiv.setAttribute("heigt", "400px")

+ 39 - 0
js12/canvas/index.css

@@ -0,0 +1,39 @@
+* {
+    box-sizing: border-box;   
+    margin: 0;
+    padding: 0;
+}
+
+body {
+    margin: 0;
+    padding: 0;
+    font-size: 20px;
+}
+
+ button{
+    width: 100%;
+    font-size: 2em;
+}
+input, button, select{
+    width: 100%;
+    font-size: 2em;
+}
+
+table {
+    border: 1px;
+    border-collapse: collapse;
+}
+
+td,th {
+    border: 1px solid black;
+}
+
+label {
+    font-size: 30px;
+    font-family: arial, sans-serif;
+    padding: 5px 5px 5px;
+}
+
+/*div#content {*/
+    /*display: none;*/
+/*}*/

+ 30 - 0
js12/canvas/index.html

@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta charset="utf-8">
+        <meta name="viewport" content="width=device-width">
+        <title>eval</title>
+        <link href="index.css" rel="stylesheet" type="text/css" />
+        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
+        <script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.1.3/papaparse.min.js"></script>
+        <script src="http://gitlab.a-level.com.ua/gitgod/nanobind/raw/master/static/nb.js"></script>
+    </head>
+    <body>
+        <canvas id='canvas' width=400 height=400></canvas> 
+        <!--<br/>-->
+        <!--<button id='+'>+</button>-->
+         <input type="button" value="undo" id="undo">
+         <input type="button" value="delete" id="delete">
+         <input type="button" value="Save Image" id="to-data-url">
+        <input type='color' id='color'>
+
+        <select id='tool'>
+            <option value='circle'>Circle</option>
+            <option value='line' >Line</option>
+            <option value='rectangle' >Rectangle</option>
+        </select>
+        <label for="size">Size(px): </label>
+        <input type="number" id="size">
+        <script src="index.js"></script>
+    </body>
+</html>

+ 278 - 0
js12/canvas/index.js

@@ -0,0 +1,278 @@
+var canvas = document.getElementById('canvas')
+var ctx    = canvas.getContext('2d')
+
+var width  = canvas.width;
+var height = canvas.height;
+
+function Drawable(){
+   Drawable.prototype.addInstance(this);   
+}
+
+
+Drawable.prototype.draw = function(){};
+Drawable.prototype.instances = [];
+Drawable.prototype.selected = [];
+Drawable.prototype.addInstance = function(item){
+    Drawable.prototype.instances.push(item);
+}
+
+Drawable.prototype.drawAll = function(){
+    ctx.clearRect(0,0,width,height);
+    for(var i = 0; i<Drawable.prototype.instances.length;i++){
+
+        Drawable.prototype.instances[i].draw(true);
+    }
+}
+
+
+Drawable.prototype.forAll = function(callback){
+    for(var i = 0; i<Drawable.prototype.instances.length;i++){
+        callback(Drawable.prototype.instances[i])
+    }
+}
+
+Drawable.prototype.forSelected = function(callback){
+    for(var i = 0; i<Drawable.prototype.selected.length;i++){
+        callback(Drawable.prototype.selected[i])
+    }
+}
+
+function Circle(x,y,radius,color, name){
+  Drawable.apply(this);
+
+  this.coords   = {x: x || 0, y: y || 0}  
+  this.radius   = radius || 10;
+  this.color    = color  || "red";  
+  this.name = name;
+}
+
+Circle.prototype = Object.create(Drawable.prototype);
+Circle.prototype.constructor = Circle;
+
+Circle.prototype.draw = function(){
+  console.log('DRAW');
+  ctx.beginPath();
+  ctx.arc(this.coords.x, this.coords.y, this.radius, 0, 2 * Math.PI, false);
+  ctx.fillStyle = this.color;
+  ctx.closePath()
+  ctx.fill();
+}
+
+Circle.prototype.coords = {x: null, y: null};
+Circle.prototype.radius   = null;
+Circle.prototype.color  = "black";
+
+
+function Line(x1,y1,x2,y2,color,lineWidth){
+  Drawable.apply(this);
+
+  this.x1 = x1;
+  this.y1 = y1;
+  this.x2 = x2;
+  this.y2 = y2;
+  this.lineWidth = lineWidth;
+  this.color    = color  || "red";  
+
+}
+
+Line.prototype = Object.create(Drawable.prototype);
+Line.prototype.constructor = Line;
+
+Line.prototype.draw = function(isShadow){
+    ctx.beginPath()
+    ctx.moveTo(this.x1,this.y1);
+    ctx.lineTo(this.x2,this.y2);
+    ctx.strokeStyle= this.color;
+    ctx.lineWidth = this.lineWidth;
+    ctx.stroke();
+    ctx.closePath()
+}
+
+function Rectangle(x1,y1,x2,y2,color,lineWidth){
+	Drawable.apply(this)
+
+	this.x1 = x1;
+	this.y1 = y1;
+	this.x2 = x2;
+	this.y2 = y2;
+	this.color = color;
+	this.lineWidth = lineWidth;
+}
+Rectangle.prototype = Object.create(Drawable.prototype);
+Rectangle.prototype.constructor = Rectangle;
+Rectangle.prototype.draw = function(){
+	ctx.beginPath();
+	ctx.rect(this.x1,this.y1,this.x2,this.y2);
+	ctx.fillStyle = this.color;
+	ctx.fill();
+	ctx.closePath();
+
+}
+
+
+
+//ctx.moveTo(0,0);
+//ctx.lineTo(width, height);
+//ctx.strokeStyle="#FF0000";
+//ctx.stroke();
+//ctx.closePath()
+//
+
+var lineStartCoords = {x: null, y: null};
+
+function draw(evt){
+	if(event.ctrlKey){
+		return;
+	}
+    var tool = document.getElementById('tool').value;
+    var obj;
+    if (tool == 'line'){
+        if (evt.type == 'mousedown'){
+            lineStartCoords = {x: evt.clientX, y: evt.clientY};
+        }
+
+        if (evt.type == 'mouseup'){
+            obj = new Line(lineStartCoords.x, lineStartCoords.y, evt.clientX, evt.clientY, document.getElementById('color').value, document.getElementById("size").value)
+        }
+        if(evt.type == 'mousemove'){
+        	
+        	obj = new Line(lineStartCoords.x, lineStartCoords.y, evt.clientX, evt.clientY, document.getElementById('color').value, document.getElementById("size").value)
+        	Line.prototype.instances.splice(-1,1);
+        	Line.prototype.drawAll();
+        }
+    }
+    else if (tool == 'circle'){
+        obj = new Circle(evt.clientX,evt.clientY,document.getElementById("size").value,document.getElementById('color').value, "circle")
+    }
+    else if (tool == 'rectangle'){
+    	if (evt.type == 'mousedown'){
+            lineStartCoords = {x: evt.clientX, y: evt.clientY};
+        }
+
+        if (evt.type == 'mouseup'){
+            obj = new Rectangle(lineStartCoords.x, lineStartCoords.y, evt.clientX - lineStartCoords.x, evt.clientY - lineStartCoords.y, document.getElementById('color').value, document.getElementById("size").value)
+        }
+        if(evt.type == 'mousemove'){
+        	
+        	obj = new Rectangle(lineStartCoords.x, lineStartCoords.y, evt.clientX - lineStartCoords.x, evt.clientY - lineStartCoords.y, document.getElementById('color').value, document.getElementById("size").value)
+        	Line.prototype.instances.splice(-1,1)
+        	Drawable.prototype.drawAll();
+        	
+        }
+    }
+    obj.draw()
+}
+
+canvas.onmousedown = draw
+canvas.onmousemove = function(evt){
+	var tool = document.getElementById('tool').value;
+    if (evt.buttons & 1){
+        draw(evt);
+    }
+}
+
+canvas.onmouseup = draw
+
+
+//document.getElementById('color').onchange = function(){
+    //Drawable.prototype.forAll(function(item){
+        //item.color = document.getElementById('color').value;
+    //})
+    //Drawable.prototype.drawAll();
+//}
+
+var undo = document.getElementById("undo");
+undo.onclick = function(){
+	Drawable.prototype.instances.splice(-2,2)
+	Drawable.prototype.drawAll();
+	console.log(Drawable.prototype.instances)
+}
+
+
+
+
+function hexToRgbA(hex){
+    var c;
+    if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
+        c= hex.substring(1).split('');
+        if(c.length== 3){
+            c= [c[0], c[0], c[1], c[1], c[2], c[2]];
+        }
+        c= '0x'+c.join('');
+        return 'rgba('+[(c>>16)&255, (c>>8)&255, c&255].join(',')+',0.3)';
+    }
+    throw new Error('Bad Hex');
+}
+
+canvas.onclick = function(event){
+	if(!event.ctrlKey){
+		return;
+	}
+	
+	Drawable.prototype.forAll(function(obj){
+
+		var x = event.clientX;
+		var y = event.clientY;
+		if(obj.name === "circle"){
+			if(obj.coords.x - obj.radius < x && x < obj.coords.x + obj.radius && obj.coords.y - obj.radius < y && y < obj.coords.y + obj.radius)
+			{
+				obj.color = "red";
+				Drawable.prototype.selected.push(obj);
+				
+				Drawable.prototype.drawAll();
+			}
+		}
+		if((obj.x1 < x && x < obj.x2 + obj.x1 && obj.y1 < y && y < obj.y2 + obj.y1) || (obj.x1 < x && x < obj.x2 && obj.y1 < y && y < obj.y2)){
+			obj.color = "red";
+			Drawable.prototype.selected.push(obj);
+			
+			Drawable.prototype.drawAll();
+		}	
+	});
+}
+
+document.getElementById('color').onchange = function(){
+	Drawable.prototype.forSelected(function(obj){
+	obj.color = document.getElementById('color').value;
+	Drawable.prototype.drawAll();
+})
+}
+document.getElementById("size").onchange = function(){
+	Drawable.prototype.forSelected(function(obj){
+	obj.lineWidth = document.getElementById('size').value;
+	obj.radius = document.getElementById('size').value;
+	Drawable.prototype.drawAll();
+})
+
+}
+
+document.getElementById("delete").onclick = function(event){
+	for (var i = 0; i < Drawable.prototype.instances.length; i++) {
+		if(Drawable.prototype.instances[i].color === "red")
+			delete Drawable.prototype.instances[i];
+		Drawable.prototype.drawAll()
+	}
+	console.log(Drawable.prototype.instances)
+	console.log(Drawable.prototype.selected)
+
+}
+
+var save = document.getElementById("to-data-url");
+
+save.onclick = function(){
+  var xmlhttp = new XMLHttpRequest();
+  xmlhttp.open("POST", "http://students.a-level.com.ua:10012");
+
+
+  var dataURL = canvas.toDataURL();
+  console.log(dataURL);
+  var resImg = "<img src="+dataURL+">"
+  xmlhttp.onreadystatechange = function(){
+    if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
+      console.log(xmlhttp.responseText);
+    }
+  }
+  console.log(xmlhttp.resImg);
+  xmlhttp.send(JSON.stringify({ func: 'addMessage', nick: "d", message: resImg}))
+}
+

+ 15 - 0
js12/emoji.js

@@ -0,0 +1,15 @@
+var emojiBtn = document.getElementById("emoji-button");
+var emoji = document.getElementById("emoji");
+var bool = true;
+emojiBtn.onclick = function(){
+	if(bool){
+		emoji.style.display = "inline-block";
+		bool = false;
+	}
+	else{
+		emoji.style.display = "none";
+		bool = true;
+
+	}
+	
+}

+ 85 - 0
js12/index.html

@@ -0,0 +1,85 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+	<meta charset="UTF-8">
+	<title>Document</title>
+		<meta charset="utf-8">
+        <meta name="viewport" content="width=device-width">
+        <title>eval</title>
+        <link href="index.css" rel="stylesheet" type="text/css" />
+        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
+        <script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.1.3/papaparse.min.js"></script>
+        <script src="http://gitlab.a-level.com.ua/gitgod/nanobind/raw/master/static/nb.js"></script>
+	<style>
+		input {
+			width: 100%;
+			height: 30px;
+			font-size: 20px;
+		}
+		body {
+			font-size: 40px;
+			font-family: arial, sans-serif;
+		}
+		.container {
+			position: fixed;
+			width: 100%;
+			background-color: #fff;
+			top: 0px;
+			z-index: 10;
+			font-size: 20px;
+		}
+		
+	</style>
+</head>
+<body>
+
+	<div class="container">
+		<label for="input1">nick:</label><br>
+		<input type="text" id="input1"><br>
+		<label for="input2">message:</label><br>
+		<div id="emoji-block">
+			<div id="emoji-button" style="display: inline-block;">
+				<img src="https://cdn.iconscout.com/public/images/icon/free/png-512/emoji-sad-unhappy-3a93cdaae83e3bb7-512x512.png" alt="" 	width="30px" height="30px">
+			</div>
+			<div id="emoji" style="display: none">
+				<img class = "emoji-item" src="http://www.kolobok.us/smiles/light_skin/aggressive.gif" alt="img">
+				<img class = "emoji-item" src="http://www.kolobok.us/smiles/light_skin/dash3.gif" alt="img">
+				<img class = "emoji-item" src="http://www.kolobok.us/smiles/light_skin/crazy.gif" alt="img">
+				<img class = "emoji-item" src="http://www.kolobok.us/smiles/light_skin/curtsey.gif" alt="img">
+				<img class = "emoji-item" src="http://www.kolobok.us/smiles/light_skin/diablo.gif" alt="img">
+				<img class = "emoji-item" src="http://www.kolobok.us/smiles/light_skin/focus.gif" alt="img">
+				<img class = "emoji-item" src="http://www.kolobok.us/smiles/light_skin/girl_in_love.gif" alt="img">
+				<img class = "emoji-item" src="http://www.kolobok.us/smiles/light_skin/hunter.gif" alt="img">
+			</div>
+		</div>
+		<input type="text" id="input2"><br>
+		<input type="button" id="button" value="send">
+		
+	</div>
+<!--//////////////////////////////////////////////////-->
+	<div class="canvas">
+		<canvas id='canvas' width=400 height=400></canvas> 
+        <!--<br/>-->
+        <!--<button id='+'>+</button>-->
+         <input type="button" value="undo" id="undo">
+         <input type="button" value="delete" id="delete">
+         <input type="button" value="Send Image" id="to-data-url">
+        <input type='color' id='color'>
+
+        <select id='tool'>
+            <option value='circle'>Circle</option>
+            <option value='line' >Line</option>
+            <option value='rectangle' >Rectangle</option>
+        </select>
+        <label for="size">Size(px): </label>
+        <input type="number" id="size">
+        <script src="index.js"></script>
+	</div>
+	
+<!--//////////////////////////////////////////////////-->
+	<div id="result"></div>
+	<script src="main.js"></script>
+	<script src="emoji.js"></script>
+	<script src="canvas/index.js"></script>
+</body>
+</html>

+ 106 - 0
js12/main.js

@@ -0,0 +1,106 @@
+
+var myObj = 0;
+var button = document.getElementById("button");
+var result = document.getElementById('result');
+var emoji = document.querySelectorAll(".emoji-item");
+for (var i = 0; i < emoji.length; i++) {
+	emoji[i].onclick = function(){
+		var xmlhttp = new XMLHttpRequest();  
+		
+
+		xmlhttp.open("POST", "http://students.a-level.com.ua:10012");
+		xmlhttp.onreadystatechange = function(){
+			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
+				console.log(xmlhttp.responseText);
+			}
+		}
+		xmlhttp.send(JSON.stringify({ func: 'addMessage', nick: "p", message: this.getAttribute("src")}))
+		
+	}
+}
+
+
+
+//
+button.onclick = function(){
+	var xmlhttp = new XMLHttpRequest();  
+	var myNick = document.getElementById('input1').value;
+	var myMessage = document.getElementById('input2').value;
+
+	xmlhttp.open("POST", "http://students.a-level.com.ua:10012");
+	xmlhttp.onreadystatechange = function(){
+		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
+			console.log(xmlhttp.responseText);
+		}
+	}
+	xmlhttp.send(JSON.stringify({ func: 'addMessage', nick: myNick, message: myMessage}))
+	
+}
+
+//
+
+setInterval(function(){
+	
+	var xmlhttp = new XMLHttpRequest(); 
+	xmlhttp.open("POST", "http://students.a-level.com.ua:10012");
+	//xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
+	xmlhttp.onreadystatechange = function(){
+		if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
+				
+				var obj = JSON.parse(xmlhttp.responseText).data;
+				console.log(obj)
+				var resObj = Object.keys(obj);
+				myObj += obj.length;
+				for (var i = 0; i < obj.length; i++) {
+					
+					if(typeof obj[i].message != "object" && obj[i].message && obj[i].message.indexOf('youtube') > -1){
+						var iframe = document.createElement("iframe");
+						var str = "https://www.youtube.com/embed/";
+						
+						var index1 = obj[i].message.indexOf('=') + 1;
+						
+						if(obj[i].message.indexOf('&') > -1){
+							var index2 = obj[i].message.indexOf('&');
+							str2 = obj[i].message.slice(index1,index2);
+						}
+						else{
+							str2 = obj[i].message.slice(index1);
+						}
+						console.log(str2);
+						iframe.setAttribute("src",str + str2);
+						result.innerHTML += obj[i].nick + ": ";   
+						result.appendChild(iframe);
+						result.innerHTML += '<br>';   
+					}	
+					else if(typeof obj[i].message != "object" && obj[i].message && /(https?:\/\/.*\.(?:png|jpg))/i.test(obj[i].message)){ // obj[i].message.indexOf('.jpg') > -1
+						var img = document.createElement("img");
+						img.setAttribute("src",obj[i].message);
+						result.innerHTML += obj[i].nick + ": ";   
+						result.appendChild(img);
+						result.innerHTML += '<br>';   
+
+					}
+					else if(typeof obj[i].message != "object" && obj[i].message && obj[i].message.indexOf('.gif') > -1){
+						var img = document.createElement("img");
+						img.setAttribute("src",obj[i].message);
+						result.innerHTML += obj[i].nick + ": ";   
+						result.appendChild(img);
+						result.innerHTML += '<br>';   
+
+					}
+					else{
+						result.innerHTML += obj[i].nick + ": "+ obj[i].message + '<br>';  
+					}
+					 
+				}
+				
+			
+			
+		}
+	}
+	
+	xmlhttp.send(JSON.stringify({ func: 'getMessages', messageId: myObj}));
+
+
+}
+,3000);