javascript - Plotting with HTML5 Canvas -


I decided the day to start the element and I can say that it has been a nightmare to work is. I want to plot a Sean Graff. So after reading good I still can not get the original nor can he get the plot. I have tried that down (sorry for my ignorance with that tag for the first time). I wonder what this is but the code for the beginning of my life is difficult.

HTML

  & lt ;! DOCTYPE html & gt; & Lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt; Graphic on & lt; / Title & gt; & Lt; link type = "text / css" rel = "stylesheet" href = "graph.css" /> & Lt; Script type = "text / javascript" src = "graph.js" & gt; & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Canvas id = "surface" & gt; Canvas is not supported & lt; / Canvas & gt; & Lt; / Body & gt; & Lt; / Html & gt;   

CSS

  #surface {width: 300; Height: 225; Border: Point # FF 20000 1px; }   

JavScript

  window.onload = function () {var canvas = document.getElementById ("surface"); Var reference = canvas.getContext ("2D"); Arr = [0, 15, 30, 45, 60, 90, 105, 120, 135, 150, 165, 180]; var x = 0; Var y = 0; For (i = 0; i & lt; arr.length; i ++) {angle = ar [i] * (Math.PI / 180); // radian sign = monastery sin (angle); context.moveTo (x, y); context.lineTo (angle, sign); Context.stroke (); // next move x set current varibles for angle; y = sign; }}    

Because the sin x range [- 1,1] , it will only return the number between -1 and 1, and this means that the drawing you make is a dot on the screen.

Besides, I see you have an array ranging from 0 to 180. I believe that you are trying to drag the curve from 0 degrees to 180 degrees with x ? You do not really need to do this (anyway 12 digits are not enough to draw a smooth line) just do it for the loop, with the number of pieces containing the lines .

We first start by moving the point to the left canvas:

  context.moveTo (0, 100 / * somewhere between * /); // starting point   

In most cases the first point will not be in the middle but this is for sign. (You can fix this later.)

 For  (var i = 0; i  < / pre> 

This is a loop depicting curve but what should be kept inside us? Well, you can take the number returned by the sign function and increase it, vice versa, and move it in half way. I do this because the coordinate system in Javascript is 0,0 at the top left instead of the bottom left.

  var sine = Math.sin (i / scale * 2) * scale; context.lineTo (i * frag, -sine + scale); // i * frag = increase the position of x / - sign = scale = y position, flip, scaled, / i / scale * 2 = put on random scale i understand the correct scale with some math   

So this is Viola, you have successfully posted an article in javascript.

Oh, yes, do not forget to actually draw it on the canvas, because for loop has done its job:

  context.stroke ();   

Demo:


PS: I think you are trying to resize the canvas using CSS. Believe me, this will not work :) You have to define the dimension in HTML.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -