// Numbas version: exam_results_page_options {"name": "Use canvas to draw a rectangle", "extensions": [], "custom_part_types": [], "resources": [], "navigation": {"allowregen": true, "showfrontpage": false, "preventleave": false}, "question_groups": [{"pickingStrategy": "all-ordered", "questions": [{"rulesets": {}, "parts": [{"allowFractions": false, "minValue": "2(w+h)", "type": "numberentry", "maxValue": "2(w+h)", "scripts": {}, "prompt": "

What is the rectangle's perimeter, in metres?

", "correctAnswerFraction": false, "showCorrectAnswer": true, "marks": 1, "showPrecisionHint": false}, {"allowFractions": false, "minValue": "w*h", "type": "numberentry", "maxValue": "w*h", "scripts": {}, "prompt": "

What is the rectangle's area, in metres?

", "correctAnswerFraction": false, "showCorrectAnswer": true, "marks": 1, "showPrecisionHint": false}], "ungrouped_variables": ["h", "w"], "name": "Use canvas to draw a rectangle", "variable_groups": [], "variables": {"w": {"name": "w", "description": "", "definition": "random(100..200#5)", "group": "Ungrouped variables", "templateType": "anything"}, "h": {"name": "h", "description": "", "definition": "random(50..100#5)", "group": "Ungrouped variables", "templateType": "anything"}}, "variablesTest": {"maxRuns": 100, "condition": ""}, "metadata": {"licence": "Creative Commons Attribution 4.0 International", "description": "

The custom function `rectangle(width,height)` draws a rectangle with the given dimensions, along with some labels.

", "notes": "

Doesn't work in IE8, because it doesn't support the canvas element.

"}, "tags": ["canvas", "demo", "function", "graphic"], "functions": {"rectangle": {"definition": "\n var c = document.createElement('canvas');\n \$(c).attr('width',w+40).attr('height',h+40);\n var context = c.getContext('2d');\n \n //fill in rectangle with a light shade\n context.fillStyle = '#eee';\n context.fillRect(5,5,w,h);\n \n //draw outline\n context.strokeStyle = '#000';\n context.lineWidth = 3;\n context.strokeRect(5,5,w,h);\n \n //draw labels\n context.fillStyle = '#000';\n context.font = '20px sans-serif';\n var wstring = w+'m';\n var tw = context.measureText(wstring).width;\n console.log(tw);\n context.fillText(wstring,5+(w-tw)/2,5+h+25);\n \n var hstring = h+'m';\n var hw = context.measureText(hstring).width;\n context.save();\n context.translate(5+w+25,5+(h+hw)/2);\n context.rotate(-Math.PI/2);\n context.fillText(hstring,0,0);\n \n return c;\n ", "language": "javascript", "type": "html", "parameters": [["w", "number"], ["h", "number"]]}}, "question_groups": [{"pickQuestions": 0, "pickingStrategy": "all-ordered", "questions": [], "name": ""}], "preamble": {"css": "", "js": ""}, "statement": "

This rectangle is {w} metres wide and {h} metres long.

\n

{rectangle(w,h)}

", "showQuestionGroupNames": false, "type": "question", "advice": "", "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}]}]}], "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}]}