hate these ads?, log in or register to hide them
Page 1 of 118 12341151101 ... LastLast
Results 1 to 20 of 2356

Thread: <xml><thread isTerrible="true" isUnderstandable="false" /></xml> - Coding help thread

  1. #1
    NoirAvlaa's Avatar
    Join Date
    April 12, 2011
    Location
    Liverpool, laaaa
    Posts
    4,157

    <xml><thread isTerrible="true" isUnderstandable="false" /></xml> - Coding help thread

    I've been trying to figure out stuff in Javascript for hours now. I've made some headway, but I need some help now.

    All this needs to be done on the canvas in HTML5. Not allowed to use jQuery, external libraries or create my own libraries, everything needs to be done in page(Lecturer was very specific about this).

    drawCanvas() is linked to an onload event on the body, and other animations work so I know it's loading

    If anyone could give me some pointers, here's what I've got so far:-

    Global code for context, not broken:-
      Spoiler:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
    // Image array and preloader for loading images into the animations.
    var advertArray = new Array("advertfiles/ljmulogo.jpg", "advertfiles/altscreen1.jpg");
    		
    function advertPreLoader() 
    {
    	for(var i=0; i<advertArray.length; i++)
    	{
    		var newImageObj = new Image();
    		newImageObj.src=advertArray[i];
    		advertArray[i]=newImageObj;
    	}
    }
    
    
    // Global variables that apply to all other code.
    var frame_no = 1;
    var c, ctx, int;
    
    // Extra code in below spoilers inserted on this line
    
    function manageCanvas()
    {
    	if (canvasSupported())
    	{
    		c = document.getElementById("canvasAdvert");
    		ctx = c.getContext("2d");
    
    		int = setInterval(drawCanvas, Math.floor(1000/25));
    		advertPreLoader();
    	}
    }
    </head>
    
    <body onload="manageCanvas()">
    	<canvas id="canvasAdvert" height="200" width="800">
    	Your browser does not support the Canvas
    	</canvas>


    Broken code 1:-
      Spoiler:

    Code:
    // I'm trying to make an image appear from left to right without being distorted. 
    // Kind of like when you unroll a poster. 
    // It's going to have an image in front of it that will move left to right and "reveal" this image.
    // Canvas 
    
    
    
    // The function that gets called when the below if statement is true.
    function animateAltScreen1(W, H, cW, cH, no_of_frames, current_frame)
    {    
        // width, height, cropped width, cropped height, no of frames, current frame/
        
        var cropWidth = (cW - W)/no_of_frames;
        var cropHeight = (cH - H)/no_of_frames;
        
        var printWidth, printHeight;
        
        printWidth = cropWidth * (current_frame-1)
        printHeight = cropHeight * (current_frame-1)
        
        ctx.drawImage(advertArray[1], 0, 0, printWidth, printHeight, 0, 0);
    }
    
    function drawCanvas()
    {
    
                
    
    // When the animation starts with values input. if((frame_no >= 100) && (frame_no <= 125)) {
    animateAltScreen1(0, 200, 800, 200, 25, frame_no-100)
    }
    }


    Broken code 2(NOW SOLVED THANKS TO AEA):-
      Spoiler:

    Code:
    // This next one I want text to fade in. 
    // I thought that I'd be able to set the alpha channel to a variable but that doesn't seem to be working. 
    // Any ideas?
    
    
    // Global variables
    var frame_no = 1;
    var c, ctx, int;
    
    // Sytax error fixed, added extra functionality for fade in or out. Thanks Aea!
    function textFade(tPX, tPY, text, no_of_frames, current_frame, fS, in_out, style)
    {
    	if (in_out==1)
    	{
    		var alpha = (current_frame/(no_of_frames-fS));
    		
    		ctx.fillStyle = "rgba(255, 255, 255, " + alpha + ")";
    		
    		ctx.font = "bold 16px Arial";
    		ctx.fillText(text, tPX, tPY);
    	}
    	else
    	{
    		var alpha = (1-(current_frame/(no_of_frames-fS)));
    		
    		ctx.fillStyle = "rgba(255, 255, 255, " + alpha + ")";
    			
    		ctx.font = style;
    		ctx.fillText(text, tPX, tPY);
    	}
    }
    
    function drawCanvas()
    {
    
    // When the animation starts with values input. if ((frame_no >= 0) && (frame_no <=15)) { textFade(250, 50, "Hello World", 15, frame_no, 0, 0, "bold 24px Arial"); }
    }


    Fairly low quality stuff, but I'm only first year so meh. Anyone got any ideas?
    Last edited by NoirAvlaa; December 18 2014 at 09:31:03 AM.

  2. #2
    Donor Aea's Avatar
    Join Date
    April 13, 2011
    Location
    Colorado
    Posts
    13,504
    I absolutely loathe when people don't post enough to replicate what they're trying to do. Here's my fixes to the second one.

    Syntax Fixes
    Code:
    // This next one I want text to fade in. 
    // I thought that I'd be able to set the alpha channel to a variable but that doesn't seem to be working. 
    // Any ideas?
    
    
    // Global variables
    var frame_no = 1;
    var c, ctx, int;
    
    
    function textFade(tPX, tPY, text, no_of_frames, current_frame, fS)
    {
        var alpha = (no_of_frames/(current_frame-fS))
        
        ctx.fillStyle = "rgba(255, 255, 255, " + alpha + ")";
        
        ctx.font = "bold 16px Arial";
        ctx.fillText(text, tPX, tPY);
    }
    
    
    function drawCanvas()
    {
    // When the animation starts with values input.
    if ((frame_no >= 0) && (frame_no <=15))
    {
        textFade(250, 50, "Hello World", 15, frame_no, 0);
    }
    }
    For the first one advertArray seems to come out of nowhere, and ctx is never defined.

  3. #3
    NoirAvlaa's Avatar
    Join Date
    April 12, 2011
    Location
    Liverpool, laaaa
    Posts
    4,157
    Whoops, I'll update with the missing bits when I get back to my pc... Sorry about that.

    E- Updated, lemme know if I've not included anything I should have...
    Last edited by NoirAvlaa; February 23 2013 at 11:47:48 PM.

  4. #4
    Qwert's Avatar
    Join Date
    April 10, 2011
    Location
    Elsewhere
    Posts
    1,827
    Quote Originally Posted by I Legionnaire View Post
    http://ideone.com/G9UpKG

    Hurr, I'm in an intro to C++ course. For this assignment we're supposed to print max and min values for int, short, and long in decimal, hex, and octal.
    Then we're supposed to print the decimal value of each variable with max +1 and min -1.

    Is there an easier way to this than how I've done it, and am I producing the overflow correctly? I get the sneaking suspicion that my variables are in octal. :/
    IIRC cout will keep whatever settings until you change them. You last changed int output to octal, so the rest are in octal. I think all you need is a

    Code:
    std::cout << std:: dec;
    
    //Before this
    intlrg = intlrg +=1 ;

  5. #5
    Donor Tellenta's Avatar
    Join Date
    April 9, 2011
    Posts
    16,062
    Why would anuYone use octal?

    fone poastin

  6. #6
    Qwert's Avatar
    Join Date
    April 10, 2011
    Location
    Elsewhere
    Posts
    1,827
    Quote Originally Posted by Tellenta View Post
    Why would anuYone use octal?

    fone poastin
    Its the largest 2n base that only uses Arabic numerals?

  7. #7
    Donor Tellenta's Avatar
    Join Date
    April 9, 2011
    Posts
    16,062
    Quote Originally Posted by Qwert View Post
    Quote Originally Posted by Tellenta View Post
    Why would anuYone use octal?

    fone poastin
    Its the largest 2n base that only uses Arabic numerals?
    I'm p. sure everything does the hexadecimal dance these days. Octal is outdated and obsolete.

    I could be wrong though.

  8. #8
    Donor Snake's Avatar
    Join Date
    April 10, 2011
    Posts
    1,411
    paging ryry

  9. #9
    Super Moderator Global Moderator QuackBot's Avatar
    Join Date
    March 7, 2012
    Posts
    20,806
    Quote Originally Posted by NoirAvlaa View Post
    whoops, i'll update with the missing bits when i get back to my pc... sorry about that. e- updated, lemme know if i've not included anything i should have...
    I've not have a vagina?

  10. #10
    Super Moderator Global Moderator QuackBot's Avatar
    Join Date
    March 7, 2012
    Posts
    20,806
    Quote Originally Posted by NoirAvlaa View Post
    i've been trying to figure out stuff in javascript for hours now. i've made some headway, but i need some help now. all this needs to be done on the canvas in html5. not allowed to use jquery, external libraries or create my own libraries, everything needs to be done in page(lecturer was very specific about this). drawcanvas() is linked to an onload event on the body, and other animations work so i know it's loading if anyone could give me some pointers, here's what i've got so far:- global code for context, not broken:-
      Spoiler:
    Code:
     <!doctype html> <html lang="en"> <head> // image array and preloader for loading images into the animations. var advertarray = new array("advertfiles/ljmulogo.jpg", "advertfiles/altscreen1.jpg"); function advertpreloader() { for(var i=0; i<advertarray.length; i++) { var newimageobj = new image(); newimageobj.src=advertarray[i]; advertarray[i]=newimageobj; } } // global variables that apply to all other code. var frame_no = 1; var c, ctx, int; // extra code in below spoilers inserted on this line function managecanvas() { if (canvassupported()) { c = document.getelementbyid("canvasadvert"); ctx = c.getcontext("2d"); int = setinterval(drawcanvas, math.floor(1000/25)); advertpreloader(); } } </head> <body onload="managecanvas()"> <canvas id="canvasadvert" height="200" width="800"> your browser does not support the canvas </canvas>
    broken code 1:-
      Spoiler:
    Code:
     // i'm trying to make an image appear from left to right without being distorted. // kind of like when you unroll a poster. // it's going to have an image in front of it that will move left to right and "reveal" this image. // canvas // the function that gets called when the below if statement is true. function animatealtscreen1(w, h, cw, ch, no_of_frames, current_frame) { // width, height, cropped width, cropped height, no of frames, current frame/ var cropwidth = (cw - w)/no_of_frames; var cropheight = (ch - h)/no_of_frames; var printwidth, printheight; printwidth = cropwidth * (current_frame-1) printheight = cropheight * (current_frame-1) ctx.drawimage(advertarray[1], 0, 0, printwidth, printheight, 0, 0); } function drawcanvas() { 
    // when the animation starts with values input. if((frame_no >= 100) && (frame_no <= 125)) {
    animatealtscreen1(0, 200, 800, 200, 25, frame_no-100)
    }
    }
    broken code 2(now solved thanks to aea):-
      Spoiler:
    Code:
     // this next one i want text to fade in. // i thought that i'd be able to set the alpha channel to a variable but that doesn't seem to be working. // any ideas? // global variables var frame_no = 1; var c, ctx, int; // sytax error fixed, added extra functionality for fade in or out. thanks aea! function textfade(tpx, tpy, text, no_of_frames, current_frame, fs, in_out, style) { if (in_out==1) { var alpha = (current_frame/(no_of_frames-fs)); ctx.fillstyle = "rgba(255, 255, 255, " + alpha + ")"; ctx.font = "bold 16px arial"; ctx.filltext(text, tpx, tpy); } else { var alpha = (1-(current_frame/(no_of_frames-fs))); ctx.fillstyle = "rgba(255, 255, 255, " + alpha + ")"; ctx.font = style; ctx.filltext(text, tpx, tpy); } } function drawcanvas() { 
    // when the animation starts with values input. if ((frame_no >= 0) && (frame_no <=15)) { textfade(250, 50, "hello world", 15, frame_no, 0, 0, "bold 24px arial"); }
    }
    fairly low quality stuff, but i'm only first year so meh. anyone got any ideas?
    Or on a lot of the lore.

  11. #11
    Muffinsrevenger's Avatar
    Join Date
    April 9, 2011
    Location
    Snuffbox
    Posts
    2,703
    I just realized that for quackers coding stuff must actually qualify as porn

  12. #12
    Donor Sparq's Avatar
    Join Date
    April 11, 2011
    Location
    Strayastan
    Posts
    8,659
    Quote Originally Posted by Muffinsrevenger View Post
    I just realized that for quackers coding stuff must actually qualify as porn
    Or at least a sex education video.

  13. #13
    Ophichius's Avatar
    Join Date
    December 15, 2011
    Location
    Hedonistic Imperative
    Posts
    5,218
    Quote Originally Posted by Tellenta View Post
    Why would anuYone use octal?

    fone poastin
    Because the teacher learned octal and goddammit so will you!

    Edit: Taking a stab at Noir's code. General comments first, then if I get it fixed and working specific solutions.

    First off, if your teacher doesn't shoot you for terrible coding style, I will. Single-character variables are terrible, you should feel terrible for using them, and you'll probably live longer if you stop. (Being beaten to death with your own spleen is the standard punishment for such hideous code practices.)

    This isn't Poland, we aren't in the midst of the great vowel famine of '48. Use descriptive goddamn variables and labels.

    Part of why things aren't working is that you're trying to use the 'int' keyword as a variable. Even if this -does- work on your specific implementation, it's really fucking bad form and a headache waiting to happen. As an aside, what editor are you using for this? You should be using something with syntax highlighting.

    That's it for now, will try and make this work when I'm not running 30+ hours out from my last nap.

    -O
    Last edited by Ophichius; April 12 2013 at 05:03:28 PM.
    I thought what I'd do was, I'd pretend I was one of those Thukkers, that way I wouldn't have to have any goddamn stupid useless conversations with anybody.
    Failing the Voight-Kampff test, one tortoise at a time.

  14. #14
    Pacefalm's Avatar
    Join Date
    April 9, 2011
    Posts
    6,353
    Basically: "you can name your variables whatever you like, but you cant name them whatever you like"
    Also semi necro it seems
    Quote Originally Posted by QuackBot View Post
    I'm doing my best. Well. Not really.

  15. #15
    Donor Tellenta's Avatar
    Join Date
    April 9, 2011
    Posts
    16,062
    Quote Originally Posted by Pacefalm View Post
    Basically: "you can name your variables whatever you like, but you cant name them whatever you like"
    Also semi necro it seems
    I think he was going for the 'name the variables in a way you will not forget what they do and have to find out the hatd way"

    fone poastin

  16. #16
    Super Moderator Global Moderator QuackBot's Avatar
    Join Date
    March 7, 2012
    Posts
    20,806
    Quote Originally Posted by Ophichius View Post

    because the teacher learned octal and goddammit so will you!

    edit: taking a stab at noir's code. general comments first, then if i get it fixed and working specific solutions.

    first off, if your teacher doesn't shoot you for terrible coding style, i will. single-character variables are terrible, you should feel terrible for using them, and you'll probably live longer if you stop. (being beaten to death with your own spleen is the standard punishment for such hideous code practices.)

    this isn't poland, we aren't in the midst of the great vowel famine of '48. use descriptive goddamn variables and labels.

    part of why things aren't working is that you're trying to use the 'int' keyword as a variable. even if this -does- work on your specific implementation, it's really fucking bad form and a headache waiting to happen. as an aside, what editor are you using for this? you should be using something with syntax highlighting.

    that's it for now, will try and make this work when i'm not running 30+ hours out from my last nap.

    -o
    No, goddammit, i don't agree with it.

  17. #17
    Lana Torrin's Avatar
    Join Date
    April 13, 2011
    Location
    Bonding around
    Posts
    17,806
    Quote Originally Posted by QuackBot View Post
    Quote Originally Posted by Ophichius View Post

    because the teacher learned octal and goddammit so will you!

    edit: taking a stab at noir's code. general comments first, then if i get it fixed and working specific solutions.

    first off, if your teacher doesn't shoot you for terrible coding style, i will. single-character variables are terrible, you should feel terrible for using them, and you'll probably live longer if you stop. (being beaten to death with your own spleen is the standard punishment for such hideous code practices.)

    this isn't poland, we aren't in the midst of the great vowel famine of '48. use descriptive goddamn variables and labels.

    part of why things aren't working is that you're trying to use the 'int' keyword as a variable. even if this -does- work on your specific implementation, it's really fucking bad form and a headache waiting to happen. as an aside, what editor are you using for this? you should be using something with syntax highlighting.

    that's it for now, will try and make this work when i'm not running 30+ hours out from my last nap.

    -o
    No, goddammit, i don't agree with it.
    No surprise that quackbot has an opinion on coding style.
    Quote Originally Posted by lubica
    And her name was Limul Azgoden, a lowly peasant girl.

  18. #18
    Ophichius's Avatar
    Join Date
    December 15, 2011
    Location
    Hedonistic Imperative
    Posts
    5,218
    Quote Originally Posted by Tellenta View Post
    Quote Originally Posted by Pacefalm View Post
    Basically: "you can name your variables whatever you like, but you cant name them whatever you like"
    Also semi necro it seems
    I think he was going for the 'name the variables in a way you will not forget what they do and have to find out the hatd way"

    fone poastin
    A mix of this and 'name variables in a way that other people can understand your code without a massive headache'.

    -O
    I thought what I'd do was, I'd pretend I was one of those Thukkers, that way I wouldn't have to have any goddamn stupid useless conversations with anybody.
    Failing the Voight-Kampff test, one tortoise at a time.

  19. #19
    dpidcoe's Avatar
    Join Date
    June 29, 2011
    Location
    San diego
    Posts
    4,908
    Quote Originally Posted by Pacefalm View Post
    Basically: "you can name your variables whatever you like, but you cant name them whatever you like"
    Also semi necro it seems
    You can name your variable whatever you like, but don't expect the people who have to read your code later to not beat you up over dumb variable names. Very similar to the concept of free speech tbh.


    Also, a surprising number of people in the computer science program at school don't understand the following two style conventions:
    - variables should be named such that they're almost self documenting
    - comments should assume that the person reading them understands how to code, but doesn't understand wtf you were doing. SO leave comments that explain what you did/why you did it, rather than what each line of code is doing, e.g. this is a completely useless set of comments:
    Code:
    for(int i = 1; i < 27; i++) //loop through the array 26 times
    {
         alphabet[i] = (char)(64+i); //fill alphabet[i]
    }
    This is a better set of comments:
    Code:
    for(int i = 1; i < 27; i++) //alphabet[] is size 26 and 1-indexed
    {
         alphabet[i] = (char)(64+i); //64+1 = 'a', 64+2 = 'b', etc.
    }

  20. #20

    Join Date
    April 9, 2011
    Posts
    337
    Comments in general cause more harm than good.
    I can't tell you how many time I've looked at a piece of code where the comments say something completely different than what the code actually does. Usually because the code has been refactored but the comments haven't. It just ends up making the whole thing harder to understand.

    You shouldn't need comments in the first place. If your code needs explaining than that is a clear indiction that you should rewrite it to be more clear and understandable. Use good class, method, and variable names and don't do a million things in a single method, then you really shouldn't ever need to comment anything.

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •